Reputation: 742
The following method saves an object of type Microsoft.Exchange.WebServices.Data.EmailMessage
on disk
private void SaveMailOnDisk(Microsoft.Exchange.WebServices.Data.EmailMessage email, string pathLocation)
{
FileStream fs = null;
BinaryWriter sw = null;
try
{
email.Load(new PropertySet(ItemSchema.MimeContent));
MimeContent mc = email.MimeContent;
fs = new FileStream(pathLocation, FileMode.Create);
// mc.CharacterSet Is UTF8
sw = new BinaryWriter(fs, System.Text.Encoding.UTF8);
sw.Write(mc.Content, 0, mc.Content.Length);
sw.Flush();
}
catch { }
finally
{
sw?.Close();
fs?.Close();
}
}
The file is succesfully saved on disk but when I try to open it using Outlook 2016
I receive the following error:
We can't open path-to-msg-file. It's possible that the file is already open, or you don't have permissions to open it
Is there any way to save it on disk as .msg
file. I mention that this method works for .eml
files but I need .msg
format.
Upvotes: 1
Views: 5176
Reputation: 66215
Keep in mind that MIME format is not native to Exchange and you might lose MAPI-specific properties if you convert an Exchange item to the MIME (EML) format.
That being said, Outlook will be happy to open an EML file just as easily as an MSG file, so you are not gaining anything by converting EML to MSG.
If you want to preserve all MAP-specific properties, you would need to export using the Fast Transfer Stream format (FTS) - this is the format produced by the ExportItems
EWS operation (see https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/exportitems-operation and https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-export-items-by-using-ews-in-exchange)
FTS format preserves all MAPI properties, but its internal structure is not documented. If you want to convert FTS data to an MSG file, you can do so using Redemption (I am its author) - create an instance of the RDOSession object, call RDOSession.CreateMessageFromMsgFile
to create a new MSG file, call RDOMail.Import(..., olFts)
to import the FTS data, then call RDOMail.Save
.
Upvotes: 1
Reputation: 1150
There is no native support for .msg
files using EWS. It's strictly just Outlook format.
This 3rd party library could satisfy your requirement however.
Regarding the error message you are getting, I would advise you to try updating your Outlook 2016 to the latest update.
Upvotes: 0