Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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("[email protected]", "NameÆØÅ");
mailMessage.To.Add(new MailAddress("[email protected]", "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

like image 781
Morten Schmidt Avatar asked Apr 05 '13 10:04

Morten Schmidt


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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;
    }
}
like image 66
Dennis Gorelik Avatar answered Sep 19 '22 18:09

Dennis Gorelik