I'm trying to use the code below to send messages via System.Net.Mail
and am sometimes getting subjects like '=?utf-8?B?W3AxM25dIEZpbGV...'
(trimmed). This is the code that's called:
MailMessage message = new MailMessage()
{
From = new MailAddress("[email protected]", "Service"),
BodyEncoding = Encoding.UTF8,
Body = body,
IsBodyHtml = true,
ReplyTo = new MailAddress("[email protected]"),
SubjectEncoding = Encoding.UTF8
};
foreach (string emailAddress in addresses)
{
message.To.Add(new MailAddress(emailAddress.Trim(), "Person"));
}
message.Subject = subject;
I'd like to emphasize that this does not happen all of the time.
What am I doing wrong?
When your subject contains characters outside the ASCII range, then the mailing software must encode them (RFC2822 mail does not permit non-ASCII characters in headers). There are two ways to do this:
"=?utf-8?Q"
)"=?utf-8?B"
)It appears that the framework has figured that the Base64 encoding is more efficient (=shorter) than the quoted-printable encoding. This makes sense when your subject contains relatively many characters outside the ASCII range.
To answer your question: You're doing nothing wrong. That's how internet mail with non-ASCII characters is supposed to look like. Of course, the software that reads such mail should detect and decode such subject fields.