Attachments are outlook messages

Hi, I like your attachment example :

foreach (MapiAttachment attachment in msg.Attachments)

attachment.Save(attachment.DisplayName);

However, if my attachments are outlook messages, they do not open correctly.

In MAPI, there are attachment properties, PR_ATTACH_METHOD to determine what kind of attachment is in the message.

I see your example of saving an embedded attachment:

foreach (MapiAttachment attachment in msg.Attachments)

{

MapiMessage attMsg = MapiMessage.FromProperties(

attachment.ObjectData.Properties);

MailMessageInterpretor mi = MailMessageInterpretorFactory.Instance.GetIntepretor(

attMsg.MessageClass);

string attachmentName = string.Format("{0}.eml",attachment.DisplayName);

FileStream fs = new FileStream(

attachmentName,

FileMode.Create);

mi.Save(attMsg, fs, Aspose.Network.Mail.MessageFormat.Eml);

}

But, how can I extract an attachment property to determine which kind of attachment it is: PR_ATTACH_DATA_OBJ or PR_ATTACH_DATA_BIN in order to know which way to save my attachment?

Also, I cannot save using the MessageFormat.Msg. I get an exception thrown that it is not supported. Can you tell me if you know when that will be supported?

Thanks,

Karen Schmidt

Hi Karen,

Thanks for your inquiry.

Using above method, saving to msg format is not supported. Nested msg attachments can be saved using the following code:

MapiMessage eml = MapiMessage.FromFile(@“E:\Data\Aspose\temp\testemail with msg as attachment.msg”);
int count = 1;
foreach (MapiAttachment att in eml.Attachments)
{
// outlook msg attachments
if (att.ObjectData != null && att.ObjectData.IsOutlookMessage)
{
MapiMessage nestedAtt = MapiMessage.FromProperties(att.ObjectData.Properties);
Console.WriteLine("Nested Attachment - Subject: " + nestedAtt.Subject);
nestedAtt.Save(@“E:\Data\Aspose\temp\1.msg”);
count++;
}
// other attachments
else
{
Console.WriteLine(att.LongFileName);
byte[] content = att.BinaryData;
}
}

But unfortunately, there is a bug and we are working on it to fix this. It is also logged in our bug tracking system (ID: 8530). We will notify you when it gets fixed. Sorry for the inconvenience.

As an alternate solution for saving nested msg attachments, you can modify your source code as follows: It first saves nested attachments as eml and then converts eml to msg. Could you please use this approach till we fix this?

MapiMessage message = MapiMessage.FromFile(@“E:\Data\Aspose\temp\embedded.msg”);
int count = 1;
foreach (MapiAttachment att in message.Attachments)
{
// att.Save(att.Name);
if (att.ObjectData != null && att.ObjectData.IsOutlookMessage == true)
{
// create a MapiMessage object from the individual attachment
MapiMessage attMsg = MapiMessage.FromProperties(att.ObjectData.Properties);
// Create object of type MailMessageImterpretor from the above message
MailMessageInterpretor mi = MailMessageInterpretorFactory.Instance.GetIntepretor(attMsg.MessageClass);
// save the embedded message to file at disk
FileStream stream = new FileStream(@“E:\Data\Aspose\temp\att” + count.ToString() + “.eml”, FileMode.Create);
mi.Save(attMsg, stream, MessageFormat.Eml);
stream.Close();

// convert to msg
MailMessage embeddedMsg = MailMessage.Load(@“E:\Data\Aspose\temp\att” + count.ToString() + “.eml”, MessageFormat.Eml);
embeddedMsg.Save(@“E:\Data\Aspose\temp\att” + count.ToString() + “.msg”, MailMessageSaveType.OutlookMessageFormat);

count++;
}
}

The issues you have found earlier (filed as 8530;13625) have been fixed in [this update ](http://www.aspose.com/community/files/51/.net-components/aspose.network-for-.net/entry220477.aspx).

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Can you tell me how I should be saving the MapiMessages? I wasn't sure which fix you meant.Is this how I should be doing it?

foreach (MapiAttachment attachment in msg.Attachments)

{

subcount++;

if (attachment.ObjectData != null &&

attachment.ObjectData.IsOutlookMessage)

{

// get output file

string attachmentName = string.Format("{0}.eml", subcount);

string emlAttachmentFilename = string.Format(

"{0}\\{1}",

attachmentFolder,

attachmentName);

using (FileStream fs = new FileStream(

emlAttachmentFilename,

FileMode.Create))

{

// get message attachment

MapiMessage attMsg = MapiMessage.FromProperties(

attachment.ObjectData.Properties);

MailMessageInterpretor mi = MailMessageInterpretorFactory.Instance.GetIntepretor(

attMsg.MessageClass);

mi.Save(attMsg, fs, MessageFormat.Eml);

}

// convert eml to msg

string msgAttachmentFilename = emlAttachmentFilename.Replace(".eml", ".msg");

MailMessage emlMessage = MailMessage.Load(emlAttachmentFilename, MessageFormat.Eml);

MapiMessage msgMessage = MapiMessage.FromMailMessage(emlMessage);

msgMessage.Save(msgAttachmentFilename);

//File.Delete(emlAttachmentFilename);

}

Hi,

Yes, Please use the same method as you mentioned to save the embedded msg.