How to read msg attachment of msg?

Hi

I’m trying to save the attachemnts of msg Outlook emails.
That works for every kind of email except when an attachment is an outlook message again.

How can I save the attached emails as a file?

In case of an outlook mails as attachment the BinaryData is null, ObjectData has a value instad. Type: Aspose.Email.Outlook.MapiObjectProperty.
Saving attachment.ObjectData.Data did not result in a correct file.

My code:
foreach (MapiAttachment attachment in msg.Attachments)
{
Entity activityAttachment = new Entity(EntityName.activitymimeattachment.ToString());

activityAttachment.Attributes.Add(“objectid”, new EntityReference(“email”, emailId));
activityAttachment.Attributes.Add(“objecttypecode”, 4202);//email activity
activityAttachment.Attributes.Add(“subject”, attachment.DisplayName);
activityAttachment.Attributes.Add(“filename”, attachment.DisplayName);

if (attachment.BinaryData != null)
{
activityAttachment.Attributes.Add(“body”, Convert.ToBase64String(attachment.BinaryData));
}
else if (attachment.ObjectData != null && attachment.ObjectData.Data != null)
{
activityAttachment.Attributes.Add(“body”, Convert.ToBase64String(attachment.ObjectData.Data));
}
else
{
WriteLog(“Error: no content in attachment”);
continue;
}

}

Regards
Adrian

Hi Adrian,


Thanks for writing to Aspose.Email support team.

Use following sample code to save attachment msg in another msg file. This sample code saves all the attachments on disk.

static void ExtractAttachments()
{
MapiMessage msg = MapiMessage.FromFile(“TestMsgInMsg.msg”); // This is the main message
foreach (MapiAttachment attachment in msg.Attachments)
{
if (attachment.ObjectData != null && attachment.ObjectData.IsOutlookMessage)
{
// For nested email attachments - save as msg
MapiMessage msgMessage = MapiMessage.FromStream((new MemoryStream(attachment.ObjectData.Data)));
msgMessage.Save(attachment.LongFileName);
}
else
{
// For other attachments
attachment.Save(attachment.LongFileName);
}
}
}

Please feel free to write us back if you have any other queries.

Works!
Thanks a lot!
Adrian

Hi Adrian,


Thanks for the feedback and feel free to contact us if you have any additional query/inquiry regarding Aspose.Email. We will be glad to help you further.