Saving an email to a .MSG file using EWS Managed API

本小妞迷上赌 提交于 2021-01-27 07:57:38

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/41227743/saving-an-email-to-a-msg-file-using-ews-managed-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!