Skip to content

Problems with from-encoding #30

@christian489

Description

@christian489

Hi,

as I had problems when using some characters in an extended from: field (e.g. "Max Müller max.mueller@yahoo.com").
I repaced the line "buf.Append(MailMessage.From);" in SendMail() with "buf.Append(GetEncodedFromAddress());" and added the following code to SmtpSocketClient.cs

`
private object GetEncodedFromAddress()
{
var headersEncoding = MailMessage.HeadersEncoding ?? Encoding.UTF8;
if (Encoding.ASCII.Equals(headersEncoding))
{
return MailMessage.From;
}
else
{
byte[] bytes = headersEncoding.GetBytes(MailMessage.From.DisplayName);

	var encodingName = headersEncoding.BodyName.ToLower();
	string qpEncodedName = QuotedPrintableConverter.Encode(MailMessage.From.DisplayName);

	string encoded = "=?" + encodingName + "?Q?" + qpEncodedName + "?= <" + MailMessage.From.Address + ">";
	return encoded;
}

}
`

also I added the follwing class (from https://dotnet-snippets.de/snippet/quoted-printable-encoder/778) used by the function above:

`
using System.Text;

namespace AegisImplicitMail
{
public class QuotedPrintableConverter
{
private static string _Ascii7BitSigns;
private const string _equalsSign = "=";
private const string _defaultReplaceEqualSign = "=";

	/// <summary>
	/// Ctor.
	/// </summary>
	private QuotedPrintableConverter()
	{
		//do nothing
	}

	/// <summary>
	/// Encodes a not QP-Encoded string.
	/// </summary>
	/// <param name="value">The string which should be encoded.</param>
	/// <returns>The encoded string</returns>
	public static string Encode(string value)
	{
		return Encode(value, _defaultReplaceEqualSign);
	}

	/// <summary>
	/// Encodes a not QP-Encoded string.
	/// </summary>
	/// <param name="value">The string which should be encoded.</param>
	/// <param name="replaceEqualSign">The sign which should replace the "="-sign in front of 
	/// each QP-encoded sign.</param>
	/// <returns>The encoded string</returns>
	public static string Encode(string value, string replaceEqualSign)
	{
		//Alle nicht im Ascii-Zeichnsatz enthaltenen Zeichen werden ersetzt durch die hexadezimale 
		//Darstellung mit einem vorangestellten =
		//Bsp.: aus "ü" wird "=FC"
		//Bsp. mit Ersetzungszeichen "%"für das "=": aus "ü" wird "%FC"

		GetAllowedAsciiSigns();
		StringBuilder sb = new StringBuilder();
		foreach (char s in value)
		{
			if (_Ascii7BitSigns.LastIndexOf(s) > -1)
				sb.Append(s);
			else
			{

				string qp = string.Format("{0}{1}",
						_equalsSign,
						System.Convert.ToString(s, 16)).Replace(_equalsSign, replaceEqualSign);
				sb.Append(qp);
			}
		}

		return sb.ToString();
	}

	/// <summary>
	/// Gets a string which contains the first 128-characters (ANSII 7 bit).
	/// </summary>
	private static void GetAllowedAsciiSigns()
	{
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < 127; i++)
		{
			sb.Append(System.Convert.ToChar(i));
		}
		_Ascii7BitSigns = sb.ToString();
	}
}

}
`

PS: sorry, I could not get the code propperbly recogniced using the "Add code" formater

I can choose between various encodings (other than ASCII7 like "iso-8859-1") using the third parameter of System.Net.Mail.MailAddress(string address, string displayName, Encoding displayNameEncoding) when set the MimeMailMessage.From property.

Maybe you can check this and integrate such a feature in your code.

best regards, Christian

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions