With respect, that example looks like it does the exact same thing as an example earlier on in this post. That example did not work. The following code appears to work…
private MailMessage ConvertEmlAttachments(MailMessage mailMessage)
{
try
{
//Convert any .eml attachments to .msg
//Loop backwards through the attachments so we can modify the collection
for (int i = mailMessage.Attachments.Count; i > 0; i–)
{
Attachment attachmentInfo = mailMessage.Attachments[i - 1];
if (string.Compare(FileSystemLayer.GetExtension(attachmentInfo.Name), “.eml”, true) == 0)
{
//Save the attachment as .msg
using (MemoryStream stream = new MemoryStream())
{
attachmentInfo.Save(stream);
stream.Position = 0;
MailMessage attMsg = MailMessage.Load(stream, MessageFormat.Eml);
string filePath = FileSystemLayer.GetTempFileName();
FileSystemLayer.DeleteIfExists(filePath);
attMsg.Save(filePath, MailMessageSaveType.OutlookMessageFormat);
//Swap the current .eml attachment with the .msg
Attachment newAttachment = new Attachment(filePath, attachmentInfo.ContentType);
newAttachment.Name = FileSystemLayer.GetFileNameWithoutExtension(attachmentInfo.Name) + “.msg”;
mailMessage.Attachments[mailMessage.Attachments.IndexOf(attachmentInfo)] = newAttachment;
}
}
}
}
catch (Exception e)
{
_logger.Error(“Failed to pre process email before import”, e);
}
return mailMessage;
}