Mikk
Mikk

Reputation: 455

Save 'MailItem' object as .msg file

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?

Upvotes: 5

Views: 7092

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66306

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

Upvotes: 2

Toon Casteele
Toon Casteele

Reputation: 2579

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(), "");
}

Upvotes: 5

Related Questions