Open an Aspose email

Hi,

I have a email saved in .msg format (Got it directly from Outlook).

Is there any way where if I can open that email, change its properties (say TO address OR subject or body) using Aspose?

I also want to use it as an attachment in another email message.

Thanks,

Venu

Hi Venu,

Thanks for considering Aspose.

Yes, you can load a saved msg file in MailMessage object, change its properties e.g. Subject, From, To, Body etc and save it with the same file name. The properties will be updated this way.

You can also attach the msg file in another message using MailMessage.Attachments.Add() method:

Here is the sample code:

// change properties of an existing msg file

string strExistingMsg = @"E:\Data\Aspose\temp\1.msg";

// load the existing file in MailMessage

MailMessage msg = MailMessage.Load(strExistingMsg, MessageFormat.Msg);

// change the properties

msg.Subject = "NEW SUBJECT (updated by Aspose.Network)";

msg.From = "sendernew@yahoo.com";

msg.To = "recipientnew@hotmail.com";

msg.HtmlBody = "NEW BODY (udpated by Aspose.Network)";

// save it again

msg.Save(strExistingMsg, MessageFormat.Msg);

Process.Start(strExistingMsg);

// Create a new msg and attach the above msg

string strNewMsg = @"E:\Data\Aspose\temp\New.msg";

MailMessage msgNew = new MailMessage("sender@domain.com", "recipient@domain.com");

msgNew.Subject = "subject";

msgNew.HtmlBody = "body of the email msg";

msgNew.Attachments.Add(new Attachment(strExistingMsg)); // add the attachment

// save this new message file

msgNew.Save(strNewMsg, MessageFormat.Msg);

Process.Start(strNewMsg);

Thanks Saqib.



I had an unsent(draft) message in a folder. I opened it, modified it.

When I saved it…it became like a regular mail instead of keeping it unsent(draft).



Is there any way to go around this?



Thanks,

Venu

For saving the message in draft status, please use MapiMessage.SetMessageFlags() method. If you are using MapiMessage in your program, you can just call MapiMessage.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
And then call MapiMessage.Save() method.

If you are using MailMessage, then please use the following code:

// change properties of an existing msg file

string strExistingMsg = @"E:\Data\Aspose\temp\test.msg";

// load the existing file in MailMessage

MailMessage msg = MailMessage.Load(strExistingMsg, MessageFormat.Msg);

// change the properties

msg.Subject = "NEW SUBJECT (updated by Aspose.Network)";

msg.HtmlBody = "NEW BODY (udpated by Aspose.Network)";

// create instance of type MapiMessage from MailMessage

MapiMessage mapiMsg = MapiMessage.FromMailMessage(msg);

// set message flag to un-sent

mapiMsg.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);

// save it

mapiMsg.Save(strExistingMsg);