Hi,
During eml to msg conversion, it does not change the format of any embedded or attached files. The embedded/attached files from eml file appear as embedded attachments in the msg file.
Embedded eml files need to converted manually and added to the converted msg. Below would be the sample code snippet that also converts nested eml attachments to msg.
// load eml file
string strEml = @“E:\Data\Aspose\temp\4439927-11960.eml”;
//string strEml = @“E:\Data\Aspose\temp\mail.eml”;
string strMsg = @“E:\Data\Aspose\temp<span class=“kwrd”>out.msg”;
MailMessage eml = MailMessage.Load(strEml, MessageFormat.Eml);
// save to msg file
eml.Save(strMsg, MailMessageSaveType.OutlookMessageFormat);
MemoryStream stream = null;
// array to save the eml attachments in Attachment collection
Attachment[] arrEmlAtt = new Attachment[eml.Attachments.Count];
int i = 0;
// convert all eml attachments to msg
MailMessage msg = MailMessage.Load(strMsg, MessageFormat.Msg);
foreach(Attachment att in eml.Attachments)
{
// loop through all the attachments
int index = att.Name.LastIndexOf(’.’);
if (att.Name.Substring(index, 4) == “.eml”)
{
// save attachment to stream
stream = new MemoryStream();
att.Save(stream);
// load in MailMessage
stream.Position = 0;
MailMessage emlAtt = MailMessage.Load(stream, MessageFormat.Eml);
stream = new MemoryStream();
// convert to msg
emlAtt.Save(stream, MailMessageSaveType.OutlookMessageFormat);
// add as an attachment of main msg
stream.Position = 0;
msg.Attachments.Add(new Attachment(stream, att.Name + “.msg”));
}
}
// get all the attachments of eml format from the msg file
foreach (Attachment att in msg.Attachments)
{
int index = att.Name.LastIndexOf(’.’);
if (att.Name.Substring(index, 4) == “.eml”)
{
arrEmlAtt[i] = att;
// eml attachment counter
i++;
}
}
// delete eml attachments from msg file
for (int emlIndex = 0; emlIndex < i; emlIndex++)
{
msg.Attachments.Remove(arrEmlAtt[emlIndex]);
}
// save the main msg
msg.Save(strMsg, MailMessageSaveType.OutlookMessageFormat);