- 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.
- 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.
- detects if the attachment is of message type
- saves in eml format to a stream
- load the eml in another MailMessage instance and convert to msg
- save as msg to disk/stream
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)
// 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();
stream.Position = 0
// save as msg format
MailMessage attMsg = MailMessage.Load(stream, MessageFormat.Eml);
Console.WriteLine("Saved " + att.Name);
// save non-eml files using att.Save() method
catch (Exception ex)
Console.WriteLine("Error : " + ex.Message);
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.
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);
// 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)
// save messages as msg files.
MailMessage msg = client.FetchMessage(msgInboxInfo.UniqueUri);
msg.Save(msg.Subject.Replace(”:”, “”) + “.msg”, MailMessageSaveType.OutlookMessageFormat);
catch (ExchangeException ex)
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…
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.
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.