We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Extracting Forward Email as Attachment

I have to parse an email that has an attachment that was forwarded on as an embedded email which in turn has embedded attachments. I have the following code to try to process this, but when I come to the line that displays the attachedMessage.Attachments.Count, it comes back as 0.


Attached is the actual email (as saved from my Outlook sent box) that I’m trying to process and you will see that it contains an email as and attachment and that it in turn has two attachments.

Also, when I call msg.Save, it saves the email, but the attachment is “corrupted” in that I can view it, but the attachments within that embedded email are converted to their binary representation. Any help is appreciated.

var msgCollection = client.ListMessages();
var sequenceNumber = 0;
foreach (var msgInfo in msgCollection)
{
// Get
an individual email message
sequenceNumber++;
var msg = client.FetchMessage(sequenceNumber);
msg.Save(@“C:\test.msg”, MailMessageSaveType.OutlookMessageFormat);
foreach (var attachment in msg.Attachments)
{
// If the attachment is actually an embedded email message
var attachmentFileInfo = new FileInfo(attachment.Name);
if (attachmentFileInfo.Extension == “.eml”)
{
// List the attachments in the attached email
var attachedMessage = MailMessage.Load(attachment.ContentStream); Console.WriteLine(attachedMessage.Attachments.Count);

}
}
}

Hi Brian,


Thank you for writing to us.

Please have a look at the following code sample which:

  • Loads your provided sample msg file
  • Checks its attachments for any embedded msg file
  • Extracts the attachments from the embedded message file
I have tested it with your sample file and the attachments from the embedded message file are not only listed properly, but also saved to disc in proper format.

Sample Code:

MailMessage myMsg = MailMessage.Load(“FW DLV
206591194 PLT 3000 PO CO-00541130.msg”
);<o:p></o:p>

foreach (Attachment a in myMsg.Attachments)

{

//if the attachment is an embedded message

if (a.ContentType.MediaType.Equals("message/rfc822"))

{

MemoryStream ms = new MemoryStream();

//save the embedded message to stream

a.Save(ms);

//load the stream as MailMessage

MailMessage embeddedMsg = MailMessage.Load(ms);

//Get total attachments count in the embedded attachment

Console.WriteLine("Number of Attachments in Embedded message: " + embeddedMsg.Attachments.Count);

//save the attachments from the embedded message

foreach (Attachment emailAtt in embeddedMsg.Attachments)

{

emailAtt.Save(emailAtt.Name);

}

}

}