Save 'MailItem' object as .msg file

c#, outlook

Solution

mailItem.SaveAs(savepath);

Where mailItem is the Outlook MailItem and the savepath is for instance:

String savepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + filename + ".msg";

If you wish to use the MailItem subject as the filename you might want to remove invalid chars for filenames:

String filename = mailItem.Subject;
string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

foreach (char c in invalid)
{
    filename = filename.Replace(c.ToString(), "");
}

Problem

Following from the code outlined here How can I save the `MailItem` object as a `.msg` file? Or another way to put this is: How can I create a `.msg` file using the attributes(sender, cc, bcc, subject, body, etc.) of a `MailItem` object?

Original source

Related problems