3
votes

My current project runs a service on a Microsoft Exchange 2010-based email address, dedicated to apply custom rules to incoming emails.

As I am browsing through the possible C#-based solutions, EWS managed API seems like the best API to use for me. Every email action I need was found, but there is one extremely big one missing... Saving an email to a .msg file. Which is quite surprising to me given how easy of an action it is from Outlook (simply drag and drop from Outlook to any folder).

This is an absolute requirement as users keep their emails organized through drag and drop. Is there any way I've missed to do that with EWS? So far I've only found two non-EWS ways :

  • Using a third-party library which I am not sure we can afford (IndependentSoft)
  • Using a more complex method with MessageSave and outlook rules to execute a custom action (action being "run MessageSave" )

I am quite surprised that such a basic action requires so much work and would like to know, is there any easy way to save an email to a .msg file ?

Worst case scenario, is there a non-EWS API, C# based method to do so?

Thank you

Edit:

I have explored the .eml export solution. The issue is we use Outlook 2007, which does not support eml format. .msg is pretty much the requirement here

2

2 Answers

3
votes

.msg is a format, which only outlook itself uses, not the exchange server you are communicating with.

For that matter, a .eml file can be created quite easily.

See here, how you can do it.

0
votes

There is a non-EWS API, C# based method to do exactly what your looking for:

http://www.independentsoft.de/exchangewebservices/tutorial/downloadmessagetomsgfile.html

using System;
using System.IO;
using System.Net;
using Independentsoft.Exchange;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            NetworkCredential credential = new NetworkCredential("username", "password");
            Service service = new Service("https://myserver3/ews/Exchange.asmx", credential);

            try
            {
               ItemShape itemShape = new ItemShape(ShapeType.Id);
               FindItemResponse inboxItems = service.FindItem(StandardFolder.Inbox, itemShape);

               for (int i = 0; i < inboxItems.Items.Count; i++)
               {
                   Independentsoft.Msg.Message msgFile = service.GetMessageFile(inboxItems.Items[i].ItemId);
                   msgFile.Save("c:\\test\\message" + i + ".msg", true);
               }
            }
            catch (ServiceRequestException ex)
            {
               Console.WriteLine("Error: " + ex.Message);
               Console.WriteLine("Error: " + ex.XmlMessage);
               Console.Read();
            }
            catch (WebException ex)
            {
               Console.WriteLine("Error: " + ex.Message);
               Console.Read();
            }
        }
    }
 }

It offers a feature to save messages and other items as Outlook .msg files.