8

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

enter image description here

Community
  • 1
  • 1
Morten Schmidt
  • 642
  • 8
  • 22
  • Why dont you use SendEmail instead? SES allows you to compose email and it does support encoding. – Akash Kava Apr 16 '13 at 18:42
  • As i see it it doesn't support attachments. ? – Morten Schmidt Apr 17 '13 at 05:14
  • Oh yes, have you tried using StringWriter? Memory Stream may ignore encoding as it uses bytes as storage. – Akash Kava Apr 17 '13 at 07:08
  • Can you provide the minomal example which doesn't involve Amazon APIs? Like an example which generates, two different memory streams one of which is correct (for `SendRawEmail`) and another isn't. – Ark-kun Apr 19 '13 at 17:21

3 Answers3

2

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;
    }
}
Dennis Gorelik
  • 1,234
  • 1
  • 11
  • 15
1

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.

outcoldman
  • 11,584
  • 2
  • 26
  • 30
  • Ya i tried that however .NET i actually very clever about setting these to UTF-8 when necessary. In the constructor of a mail address forinstance you find: this.displayNameEncoding = displayNameEncoding ?? Encoding.GetEncoding("utf-8"); Which means if you dont define encoding it will default to UTF-8. In the set of the subject and body they have code testing whether a string contains ascii only otherwise it will try to guess encoding on its own. So if you do mail.Subject = "æøå"; and then check the mail.SubjectEncoding it will correctly already be UTF-8 – Morten Schmidt Apr 06 '13 at 16:33
0
message.BodyEncoding = message.SubjectEncoding = message.HeadersEncoding = Encoding.UTF8;
SuperUser
  • 41
  • 8