3

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

Damascus
  • 6,553
  • 5
  • 39
  • 53

2 Answers2

3

.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.

Community
  • 1
  • 1
NotTelling
  • 462
  • 3
  • 11
  • Thanks (sorry for the late answer - holidays!). I used the method to create a .eml file and I couldn't open it with Outlook! If I set outlok as the program to open it, it opens a new email with the eml file as an attachment. Any way to workaround ? – Damascus Jan 06 '17 at 16:11
  • You can change the file extension to .mht and open it in Internet Explorer (as this is to my knowledge the only browser to properly display .mht files). With this workaround, you can't see or download attachments to the E-Mail, though. If this is important to you, maybe try another mailing program (e.g. Thunderbird) to further confine the problem. – NotTelling Jan 07 '17 at 10:11
  • @Damascus have you looked at the other answers to the question. This one looks promising: http://stackoverflow.com/a/36070777/1033684- there are three possible solutions in it. – Rob Sedgwick Jan 25 '17 at 15:56
  • @Damascus do you need to access the files programmatically? If not, have you tried one of the many .eml file readers? There are even .eml to .msg converters out there. The library "Redemption" [link](http://www.dimastr.com/redemption/home.htm) can do that within your c# application with just a few lines of code. – NotTelling Jan 25 '17 at 16:53
  • Hi! I had an opportunity to try both of them but as explained in the post I would preferably like to see if there is a proper API solution rather than a paid third-party library! – Damascus Jan 25 '17 at 17:45
  • @Damascus re not being able to open the EML file in Outlook. Which version of outlook are you using? Outlook 2010 and above have native support for EML files but Outlook 2003 required [KB967346 patch](https://support.microsoft.com/en-us/help/967346/files-that-have-the.eml-file-name-extension-do-not-open-in-outlook-2003) – G Davison Jan 27 '17 at 12:33
  • @GDavison 2003 pre-patch. Unfortunately I work in a big company and can't just ask the entire company to roll out a new patch for outlook. They're planning to move to another version of outlook "soon" but my app should be ready way before that! – Damascus Jan 30 '17 at 16:07
0

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.

MadDev
  • 1,130
  • 1
  • 13
  • 29