Converting EML attachments to MSG

Is there a way to read an email from exchange load it into memory, save each attached eml file and the original email itself to msg and then save that one email to disk?


So to put it another way lets say I have an email with eml 1 attachment and that attachment has 1 eml attachment. I would like to convert the entire email to msg including each attachment and then save the original email to disk as msg.


Hi Matthew,


Thank you for contacting Aspose Support team.

Yes, you can fetch a message from Exchange server and save it to disc as MSG file. All its attachments and other contents will be saved as it is. Once the message is retrieved as MailMessage, you can then use this object to save the retrieved email as MSG or any other format such as MHT.

Sample Code:

IEWSClient client = GetAsposeEWSClient1();

ExchangeMessageInfoCollection msgsColl = client.ListMessages(client.MailboxInfo.InboxUri);
//fetch a message from server
MailMessage eml = client.FetchMessage(msgsColl[0].UniqueUri);

//now save it to disc as MSG
eml.Save(eml.Subject + “.msg”, Aspose.Email.Mail.SaveOptions.DefaultMsgUnicode);

So each email has other emails in eml format attached to them. Is there a way to convert each attachment to msg along with the parent email?

Hi,


You can parse each attachment from the retrieved MSG by checking for its media type. Please try the following code sample at your end and let us know your feedback.

Sample Code:

IEWSClient client = GetAsposeEWSClient1();

ExchangeMessageInfoCollection msgsColl = client.ListMessages(client.MailboxInfo.InboxUri);
//fetch a message from server
MailMessage eml = client.FetchMessage(msgsColl[0].UniqueUri);

//Save each attachment separately as MSG format
foreach (Attachment att in eml.Attachments)
{
if (att.ContentType.MediaType.Equals(“message/rfc822”)) //means the attachment is an email
{
MemoryStream ms = new MemoryStream();
att.Save(ms);
ms.Position = 0;

//Load in MailMessage
MailMessage attEml = MailMessage.Load(ms);

attEml.Save(attEml.Subject + “.msg”, Aspose.Email.Mail.SaveOptions.DefaultMsgUnicode);
}
}

//now save it to disc as MSG
eml.Save(eml.Subject + “.msg”, Aspose.Email.Mail.SaveOptions.DefaultMsgUnicode);