5

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?

Community
  • 1
  • 1
Mikk
  • 455
  • 1
  • 13
  • 19

2 Answers2

5
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(), "");
}
Toon Casteele
  • 2,479
  • 15
  • 25
  • 1
    You're welcome, I just wrote a pretty large outlook add-in myself which also contains this code. – Toon Casteele Mar 21 '13 at 15:29
  • I'll choose your answer for the extra info of raising the issue of invalid chars when using the subject as the filename. Thanks again Toon! – Mikk Mar 21 '13 at 16:03
2

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).

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78