Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with from-encoding #30

Open
christian489 opened this issue Mar 2, 2022 · 3 comments
Open

Problems with from-encoding #30

christian489 opened this issue Mar 2, 2022 · 3 comments

Comments

@christian489
Copy link

Hi,

as I had problems when using some characters in an extended from: field (e.g. "Max Müller [email protected]").
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

carlosbet added a commit to carlosbet/AIM that referenced this issue Jun 27, 2022
carlosbet added a commit to carlosbet/AIM that referenced this issue Jun 29, 2022
@boskoKlemenc
Copy link

boskoKlemenc commented Jul 23, 2022

Hi,

the problem is with all email address headers (from, to, cc).
I made a similar fix as you did:

    private object TransformAddress(MailAddress address)
    {
        var headersEncoding = MailMessage.HeadersEncoding ?? Encoding.UTF8;
        if (headersEncoding.Equals(Encoding.ASCII) || address.DisplayName.Trim() == "")
            return address;

        return GetEncodedAddress(address, headersEncoding);
    }

    private string GetEncodedAddress(MailAddress address, Encoding encoding)
    {
        var encodingName = encoding.BodyName.ToLower();
        return "=?" + encodingName + "?B?" + TransferEncoder.ToBase64(encoding.GetBytes(address.DisplayName)) + "?=" + " <" + address.Address + ">";
    }

And then calling this when adding headers:

buf.Append(TransformAddress(MailMessage.From)); ... buf.Append(TransformAddress(MailMessage.To[0])); ... buf.Append(TransformAddress(MailMessage.To[x])); ... buf.Append(TransformAddress(MailMessage.CC[0])); ... buf.Append(TransformAddress(MailMessage.CC[x]));

@carlosbet
Copy link
Contributor

Hi @boskoKlemenc,

You are right. Currently, in my MUA software that uses this project, just permits use the “display name” property in the “from” field. But also should be able used in any mail address field…

Although, the real problem here is not forcing the use of MimeMailAddress in front of System.Net.Mail.MailAddress. Then it would only be necessary to override the ToString() method. At this time, both classes are mixed along the code and in the client also can be use both. For making this in a good way, should be necessary to hide and override some methods in MimeMailMessage class. But I think this would produce many other changes and things to check. So for the moment, just will refactor my code similarly. Thanks for your reply.

carlosbet added a commit to carlosbet/AIM that referenced this issue Jul 26, 2022
@christian489
Copy link
Author

christian489 commented Aug 3, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants