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

Is it possible to force UTF-8 encoding for Headers ? #493

Closed
GuillaumeErgole opened this issue Jul 1, 2019 · 4 comments
Closed

Is it possible to force UTF-8 encoding for Headers ? #493

GuillaumeErgole opened this issue Jul 1, 2019 · 4 comments
Labels
enhancement New feature or request

Comments

@GuillaumeErgole
Copy link

Describe the bug
It's not really a bug but rather a question.
Is it possible to force encoding for headers ?

Platform (please complete the following information):

  • OS: Windows
  • .NET Runtime: .Net Framework 4.7
  • .NET Framework: .Net Standard 2.0
  • MimeKit Version: 2.1.3

To Reproduce
for MailboxAddress :

var mailAdress = new MailboxAddress(Encoding.UTF8, "Un texte accentué", "someone@company.com");

var actual = mailAdress.ToString(true);
// actual : "Un texte =?iso-8859-1?q?accentu=E9?=\t<someone@company.com>"
// expected : "=?utf-8?b?VW4gdGV4dGUgYWNjZW50dcOp?=\t<someone@company.com>"

and for Subject :

var formatOptions = FormatOptions.Default.Clone();
formatOptions.AllowMixedHeaderCharsets = false; // https://github.com/jstedfast/MimeKit/issues/478

var message = new MimeMessage();
//var index = message.Headers.IndexOf(HeaderId.Subject);
//message.Headers[index].SetValue(formatOptions, Encoding.UTF8, "Un texte accentué"); // https://github.com/jstedfast/MimeKit/issues/478
message.Headers.Replace(HeaderId.Subject, Encoding.UTF8, "Un texte accentué"); // https://github.com/jstedfast/MimeKit/issues/91

using (var stream = new MemoryStream())
{
	message.WriteTo(formatOptions, stream);

	stream.Position = 0;
	using (var reader = new StreamReader(stream))
	{
		var actual = reader.ReadToEnd();
		// actual : "[...]Subject: Un texte =?iso-8859-1?q?accentu=E9?=[...]"
		// expected : "[...]Subject: =?utf-8?b?VW4gdGV4dGUgYWNjZW50dcOp?=[...]"
	}
}

Expected behavior
I understand that MimeKit uses the shortest solution, but I need to force headers' encoding (base64 utf-8)

  • What is the utility of MailboxAddress.Encoding property ?
  • What is different in using Header.SetValue() with or without encoding parameter ?
@jstedfast
Copy link
Owner

Why do you need to enforce an encoding? Anything that handles UTF-8 will also handle iso-8859-1.

It seems most people who ask me this THINK that it has to be UTF-8 but in reality it doesn't.

@GuillaumeErgole
Copy link
Author

2 reasons for this :

1/ We don't no exactly why, but for some mailing campaigns we obtain a higher spam score (VadeSecure) with iso encoding than with utf-8

2/ Some clients use an external Smtp relay service which seams not respect all rfc, it needs full utf-8 encoding for "From" header

@jstedfast
Copy link
Owner

I'll look into making some changes which allow you to hard-code UTF-8.

FWIW, here are the answers to your previous questions:

  • What is the utility of MailboxAddress.Encoding property ?

That specifies the encoding to use if MimeKit can't use us-ascii or iso-8859-1 (which it is using for max compatibility, but I suppose I could make it not try to use us-ascii or latin1)

  • What is different in using Header.SetValue() with or without encoding parameter ?

Same as above.

The FormatOptions.AllowMixedHeaderCharsets property is used by the Rfc2047 encoder. Normally, the encoder tries to logically break apart words into "runs" of words that should all use the same charset (ascii, iso-8859-1, and the user-specified charset) to encode them. When that option is set to false, it fixes up the runs afterward to all use the same charset.

jstedfast added a commit that referenced this issue Jul 6, 2019
…ser-specified charset

Previously this could/would still use us-ascii and/or iso-8859-1 if
the entire header could fit within one of those charsets.

Fixes issue #493
@jstedfast jstedfast added the enhancement New feature or request label Jul 6, 2019
@GuillaumeErgole
Copy link
Author

Thank you for these changes, we'll test it as soon as possible.

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

No branches or pull requests

2 participants