Email attachments coming in as .eml instead of .msg

Hello,


We automatically import emails from an exchange server for a client. The client indicated that msg files we import into our document mgmt system have attachments that are msg but when imported into our system using Aspose, the attachments come in as .eml.

We convert the exchange mail to .msg format when we save it but do not preform any updates to the internals of the mail.

Can you explain why the attachments are coming in as .eml? Is eml the default format of exchange server?

Thank you,
John Paul

Hi John,

  1. If you save messages from Exchange Server using ExchangeClient.Save() method, the messages will be saved as eml and the attachments will also be converted in eml format.
  2. And if you save messages by first calling ExchangeClient.FetchMessage() and MailMessage.Save(file, MessageFormat.OutlookMessageFormat) methods, the messages will be saved as msg files. The message attachments will also be in msg format.
A sample application is attached that saves the message from Exchange Server in eml and msg format. The Inbox of my local Exchange Server had message with msg attachments. The saved eml and msg files are also included in the sample application.

In your case, I would recommend to use the second method to save the messages and attachments in msg format.

Hello,


We are using the second mechanism as you have suggested and this is not working. The only thing we are doing different is that we are extracting and saving the Attachments out of the MailMessage before we call mailMessage.Save(“test.msg”, MailMessageSaveType.OutlookMessageFormat);

Will this make a difference?

JP

Hi,


In a MailMessage instance, the default format of message attachments is eml. In order to save the message attachments in msg format, please try the following method, which
  1. detects if the attachment is of message type
  2. saves in eml format to a stream
  3. load the eml in another MailMessage instance and convert to msg
  4. save as msg to disk/stream
Sample code snippet:


try
{
string filename = @“test.msg”;
// this is the main msg file, loaded from stream, FetchMessage or disk
MailMessage msg = MailMessage.Load(filename, MessageFormat.Msg);
int count = 0;
// loop through all the attachments
foreach (Attachment att in msg.Attachments)
{
count++;
// if msg subject contains characters like :, /, \ etc, replace with space
// because windows cannot save files with these characters
// also save first 50 characters as file name to avoid long file names
string attFileName = att.Name.Replace(".eml", “”).Replace(":", " “).Replace(”\", " “).Replace(”/", " “).Replace(”?", “”);
if (attFileName.Length > 50)
attFileName = attFileName.Substring(0, 50);
string attExt = (att.Name.Substring(att.Name.LastIndexOf("."), 4));
// check if it is an eml attachment
// note that even if the original message contains msg attachments, they will be in eml format
// when loaded in MailMessage instance
if (attExt == “.eml”)
{
string strCount = count.ToString();
if (count < 10) strCount = “0” + strCount;
string msgFileName = “att\” + strCount + " - " + attFileName + “.msg”;

// save as eml format
MemoryStream stream = new MemoryStream();
att.Save(stream);
stream.Position = 0
// save as msg format
MailMessage attMsg = MailMessage.Load(stream, MessageFormat.Eml);
attMsg.Save(msgFileName, MailMessageSaveType.OutlookMessageFormat);
Console.WriteLine("Saved " + att.Name);
}
else
{
// save non-eml files using att.Save() method
att.Save(attFileName);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error : " + ex.Message);
}

Hi,


I have tried that but it does not alter the original MailMessage. That only allows the attachments to be extracted from the original MailMessage as .msg.

I need to be able to save the original MailMessage with its email attachments in the .msg format just like my customers would see it in Microsoft Outlook.

Kind regards,
JP

Hi,


To save the original message in msg format, with its email attachments also in msg format, you may use the following code snippet:

string mailboxUri = “https://ex07sp1/exchange/administrator”;
string username = “Administrator”;
string password = “Evaluation1”;
string domain = “litwareinc.com”;

NetworkCredential credential = new NetworkCredential(username, password, domain);
//create an exchangeclient
ExchangeClient client = new ExchangeClient(mailboxUri, credential);
try
{
// get mailbox info
ExchangeMailboxInfo mailbox = client.GetMailboxInfo();

Console.WriteLine(“Connected to exchange server.”);
Console.WriteLine(“InboxUri: " + mailbox.InboxUri);
Console.WriteLine(“Deleted Items Uri: " + mailbox.DeletedItemsUri);

// list inbox items
ExchangeMessageInfoCollection msgInboxCollection = client.ListMessages(mailbox.InboxUri);
foreach (ExchangeMessageInfo msgInboxInfo in msgInboxCollection)
{
if (msgInboxInfo.Subject != null)
{
Console.WriteLine(msgInboxInfo.Subject);
// save messages as msg files.
MailMessage msg = client.FetchMessage(msgInboxInfo.UniqueUri);
msg.Save(msg.Subject.Replace(”:”, “”) + “.msg”, MailMessageSaveType.OutlookMessageFormat);
}
}
}
catch (ExchangeException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
Console.WriteLine(“Disconnected.”);
}

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;
}

Ok. Now that we appear to have a fix for saving the attachments as .msg, I am getting a related issue that all attachments of type email are being renamed to ATT00001.msg, ATT00002.msg etc.

When browsing the email in “Microsoft Outlook Web Access”, the attachment has the correct name (all be it without an extension). When imported into our system using Aspose.Network, the name is changed to the ATT000x format.


Can you explain?

JP

Hello,


I presume this relates to issue#15949. Can you confirm and please tell me when a patch can be made available? I have an important customer live with this issue and we are losing this information for them.

Regards,
JP

Hi,


Yes, this issue is known to us and we are working on it. We will notify as soon as it gets fixed. The issue ID is 15949.

Hello,


Can I get some type of timescale on getting a patch for this problem. I have customers that are currently live with this issue and they are not happy we are losing this information.

Regards,
JP

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

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

Thank you very much.


JP