简体   繁体   中英

C# MailMessage to stream - encoding issue

Iam using Amazon SES SendRawEmail API hence i need a MailMessage as a Memorystream.

I have found several answers to the MailMessage to MemoryStream issue here on stackoverflow.

Solution 1: Using the one that uses private methods it results in wrong encoding of parts of the email: https://stackoverflow.com/a/8826833

Solution 2: No Encoding problems using this solution where you send to a pickup directory and then read it back in: https://stackoverflow.com/a/14350088

I very much dislike the fact that i need to write to a temporary file to make this work correctly. Does anyone have any idea how the pure MemoryStream solution can mess up some of the encoding.

The mail message iam testing with is this:

var mailMessage = new MailMessage();
mailMessage.Subject = "HEADER WITH SPECIAL ÆØÅ";
mailMessage.Body = "TEST";

Attachment att = new Attachment(@"C:\AttachmentWithSpecial ÆØÅ.pdf");
mailMessage.Attachments.Add(att);
mailMessage.From = new MailAddress("test@test.com", "NameÆØÅ");
mailMessage.To.Add(new MailAddress("test@test.com", "NameÆØÅ"));

To summarize:

  • If i send this message with standard SMTP it looks good.
  • If i send it using SendRawEmail it looks good if i generated the memorystream by using solution 2
  • If i send it using SendRawEmail it has encoding issues if i generated the memorystream by using solution 1.

With encoding issues i mean 'ø' showing up as

在此处输入图片说明

Did you try to explicitly specify encoding for Subject / Body and MailAddress?

I guess that when .NET writes mails to folder it can somehow to define the right encoding (or maybe File Reader somehow can find the right encoding to convert data). And in memory everything is in default encoding, which does not work for you.

Unfortunately MailMessage object is buggy and has poor interface.

The good thing is that .NET 4.5 partially fixed it with allowUnicode flag in Send() (unfortunately Send method is still private)

Below is modified "Solution 1". It encodes subject the same way as "Solution 2". .NET Framework 4.5 only.

private static MemoryStream ConvertMailMessageToMemoryStream(MailMessage message)
{
    Assembly systemAssembly = typeof(SmtpClient).Assembly;
    Type mailWriterType = systemAssembly.GetType("System.Net.Mail.MailWriter");
    const BindingFlags nonPublicInstance = BindingFlags.Instance | BindingFlags.NonPublic;
    ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor(nonPublicInstance, null, new[]  typeof(Stream) }, null);

    MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", nonPublicInstance);
    MethodInfo closeMethod = mailWriterType.GetMethod("Close", nonPublicInstance);
    using (MemoryStream memoryStream = new MemoryStream())
    {
        object mailWriter = mailWriterContructor.Invoke(new object[] { memoryStream });
        //AssertHelper.IsTrue(sendMethod.GetParameters().Length > 2, ".NET framework must be 4.5 or higher in order to properly encode email subject.");
        sendMethod.Invoke(message, nonPublicInstance, null,
            new[] { mailWriter, true, false },
            null);
        closeMethod.Invoke(mailWriter, nonPublicInstance, null, new object[] { }, null);
        return memoryStream;
    }
}
message.BodyEncoding = message.SubjectEncoding = message.HeadersEncoding = Encoding.UTF8;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM