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?
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(), "");
}
Use MailItem.SaveAs(..., olMsg) - see http://msdn.microsoft.com/en-us/library/office/bb175283(v=office.12).aspx.
Or do you mean you want to create an MSG file from the scratch without an actual MailItem object residing in one of the Outlook folders? In that case you can use Redemption (I am its author) and its RDOSession.CreateMessageFromMsgFile method (returns RDOMail object).