Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save 'MailItem' object as .msg file

Tags:

c#

outlook

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?

like image 293
Mikk Avatar asked Mar 21 '13 15:03

Mikk


People also ask

How do I save a File in MSG format?

Double-click to open the message you want to save, and on the File menu, click Save As. In the Save as dialog box, in the Folder pane, choose a folder, and then the location in that selected folder where you want to save the file. In the File name box, type a name for the file.

How do I save emails to a folder?

Move messages into a folder Select an email message. Drag and drop it into a folder. Note: To move more than one email, select an email, hold down the Shift key and select other messages, and then click, drag, and drop them into a folder.


2 Answers

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(), "");
}
like image 53
Toon Casteele Avatar answered Sep 18 '22 00:09

Toon Casteele


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

like image 28
Dmitry Streblechenko Avatar answered Sep 22 '22 00:09

Dmitry Streblechenko