System.Net.Mail and =?utf-8?B?XXXXX.... Headers

37,701

Solution 1

I came across this post when I was debugging an identical problem, and based on my further investigations I can provide an alternate explanation to Andreas's:

The problem may be that your e-mail client software (Outlook 2003, in my case) is incorrectly decoding the subject line. In other words, it's a bug in Outlook, not in .NET or your program.

If you use a subject value like this (the letter "c" repeated 256 times), it displays fine in Outlook:

subject = New String("c"c, 256)

Likewise, if you use a subject like this (the letter "c" repeated 178 times, with a Unicode non-breaking space character appended), it also displays as expected in Outlook:

subject = New String("c"c, 178) + System.Text.Encoding.UTF8.GetChars(New Byte() {194, 160})

However, the following subject displays as "=?utf-8?B"-prepended garbage in Outlook:

subject = New String("c"c, 179) + System.Text.Encoding.UTF8.GetChars(New Byte() {194, 160})

The difference is that this third subject line is 256 bytes when UTF-8-encoded. I assume that Outlook must be truncating the subject line to 255 characters before displaying it... which would be fine except that it is doing this by truncating the encoded string to 255 bytes, which cuts off the encoding terminator ("?="), making it undecodable.

This is a bug in Outlook and not your mail provider or .NET; you can see the full, untruncated UTF-8 encoded subject line in Outlook by right-clicking on the message in your message list and selecting "Options..." from the context menu, then scrolling down in the "Internet Headers" box until you see the line starting with "Subject:".

Contrary to the situation that Andreas suggests, the problem manifests itself not only when there are many non-ASCII characters, but when there is one or more non-ASCII characters and the subject line is long. A workaround might be to use a shorter subject line or to strip out all the non-ASCII characters in your subject.

(This bug was particularly tricky for me to track down because, as above, the problem data included no obvious non-ASCII characters -- just a few non-breaking spaces. These display the same as regular ASCII spaces when you print them out to the console. Moreover, if you change the value of the string variable in the Visual Studio debugger, it silently replaces them with regular spaces.)

Solution 2

The answer might be in the rest of the trimmed subject -- the section you provided decodes as "[p13n] File", but is you've any non-ASCII characters in there, then I would expect it to encode as it has

Share:
37,701
808dave
Author by

808dave

Updated on August 02, 2022

Comments

  • 808dave
    808dave almost 2 years

    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?