MSG-file in Attachments

Hi! I have a question. If I have Email with the attached MSG how can I get this MSG and work with it like a MailMessage?
Thanks.

Hi Albert,

Thank you for contacting Aspose.Email support team.

If you are loading the source message using MailMessage class, you can use the following code sample for this purpose:

Sample Code:

MailMessage msg = MailMessage.Load("Test with attachment message.msg", MessageFormat.Msg);

Attachment att = msg.Attachments[0];

if (att.ContentType.MediaType == "message/rfc822")
{
    MemoryStream ms = new MemoryStream();
    att.Save(ms);

    MailMessage attMsg = MailMessage.Load(ms);

    Console.WriteLine("From: " + attMsg.From);
    Console.WriteLine("To: " + attMsg.To);
    Console.WriteLine("Subject: " + attMsg.Subject);
}

In case you are using MapiMessage class for loading such a source message file, you can refer to the following code sample for your kind reference to achieve this:

Sample Code:

MapiMessage message = Aspose.Email.Outlook.MapiMessage.FromFile("Test with attachment message.msg");

MapiAttachmentCollection attachments = message.Attachments;

foreach (MapiAttachment attachment in attachments)
{
    if (attachment.ObjectData != null && attachment.ObjectData.IsOutlookMessage)
    {
        MapiMessage embeddedMessage = Aspose.Email.Outlook.MapiMessage.FromStream(new System.IO.MemoryStream(attachment.ObjectData.Data));

        // Now use MailMessageInterpretor for conversion of MapiMessage to MailMessage
        MailMessageInterpretor mi = MailMessageInterpretorFactory.Instance.GetIntepretor(message.MessageClass);
        MailMessage mailMsg = mi.Interpret(embeddedMessage);

        Console.WriteLine("From: " + mailMsg.From);
        Console.WriteLine("To: " + mailMsg.To);
        Console.WriteLine("Subject: " + mailMsg.Subject);
    }
}

Please feel free to write us back in case you have any other query/inquiry related to Aspose.Email. We’ll try to assist you further as soon as possible.

Thanks! It’s great!

Hi Albert,


Thank you for the feedback and please feel free to write to us in case you have any additional query/inquiry related to Aspose.Email. We’ll be glad to assist you further.