none
How to save Email to disk as *.eml or *.msg while using Exchange web services to read Inbox mail in c# RRS feed

  • Question

  • Hi Everyone,

    I am using Exchange web services to read inbox mail of a user. In below code, i want to save the EMailMessage object to disk as *.eml or *.msg.

    EMailMessage does not have any method like SaveAs() or ToByteArray().

    Please tell me how can I save email as a file.

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
                string exSvcUrl = "https://exchangeserver.dcpladmin.com/ews/exchange.asmx";
                service.Url = new Uri(exSvcUrl);
                string domain = "*****";
                string credUser = "****";
                string credPwd = "****";
                service.Credentials = new NetworkCredential(credUser, credPwd, domain);
                string impUser = "administrator@*****";
                service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, impUser);
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
                Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
                SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
                ItemView view = new ItemView(20);
                FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view);
                foreach (EmailMessage mail in findResults)
                {
                    string fileName = String.Format("{0}_{1}.eml", mail.Subject.ToString(), mail.DateTimeReceived.ToString("yyyyMMddHHmmss"));

                    byte[] contents = mail.ConversationIndex;
                    UploadMailToLibrary(contentDb, contents, fileName);
                    mail.IsRead = true;
                }


    Regards urvinit

    Monday, February 18, 2013 11:55 AM

Answers

  • EWS will only allow you to save a Message in EML format using the Messages underlying MIMEcontent eg

                PropertySet psPropSet = new PropertySet(EmailMessageSchema.MimeContent);
                ItemId itItemId = new ItemId(ewsID);
                EmailMessage emEmailMessage = EmailMessage.Bind(esExchangeService, itItemId, psPropSet);
                FileStream fsFileStream = new FileStream(FileName, FileMode.Create);
                fsFileStream.Write(emEmailMessage.MimeContent.Content, 0, emEmailMessage.MimeContent.Content.Length);
                fsFileStream.Close();
                Console.WriteLine("Message exported to " + FileName);

    The MSG file format is an Office file format (or compound OLE format) so to save a message from Exchange to a MSG file you need to use Mapi via the OOM or possibly a third party library like Redemptionhttp://www.dimastr.com/redemption/home.htm.

    Cheers
    Glen

    • Marked as answer by urvinit Tuesday, February 19, 2013 5:31 AM
    Tuesday, February 19, 2013 5:16 AM
  • Hi Glen,

    Thanks for your reply.

    I have already done this using MIMEContent as you are saying. I have used below code.

      string mailbox = String.Format("C:\\Users\\Administrator\\Documents\\Inbox"); string fileName = String.Format("{0}_{1}.eml", mail.Subject.ToString(), mail.DateTimeReceived.ToString("yyyyMMddHHmmss"));
                    string filePath = String.Format("{0}\\{1}", mailbox, fileName);
                    mail.Load(new PropertySet(ItemSchema.MimeContent));

                    MimeContent mc = mail.MimeContent;
                    FileStream oFileStream = new FileStream(filePath, FileMode.Create);

                    oFileStream.Write(mc.Content, 0, mc.Content.Length);
                    oFileStream.Close();


    Regards urvinit

    • Marked as answer by urvinit Tuesday, February 19, 2013 5:31 AM
    Tuesday, February 19, 2013 5:30 AM

All replies

  • EWS will only allow you to save a Message in EML format using the Messages underlying MIMEcontent eg

                PropertySet psPropSet = new PropertySet(EmailMessageSchema.MimeContent);
                ItemId itItemId = new ItemId(ewsID);
                EmailMessage emEmailMessage = EmailMessage.Bind(esExchangeService, itItemId, psPropSet);
                FileStream fsFileStream = new FileStream(FileName, FileMode.Create);
                fsFileStream.Write(emEmailMessage.MimeContent.Content, 0, emEmailMessage.MimeContent.Content.Length);
                fsFileStream.Close();
                Console.WriteLine("Message exported to " + FileName);

    The MSG file format is an Office file format (or compound OLE format) so to save a message from Exchange to a MSG file you need to use Mapi via the OOM or possibly a third party library like Redemptionhttp://www.dimastr.com/redemption/home.htm.

    Cheers
    Glen

    • Marked as answer by urvinit Tuesday, February 19, 2013 5:31 AM
    Tuesday, February 19, 2013 5:16 AM
  • Hi Glen,

    Thanks for your reply.

    I have already done this using MIMEContent as you are saying. I have used below code.

      string mailbox = String.Format("C:\\Users\\Administrator\\Documents\\Inbox"); string fileName = String.Format("{0}_{1}.eml", mail.Subject.ToString(), mail.DateTimeReceived.ToString("yyyyMMddHHmmss"));
                    string filePath = String.Format("{0}\\{1}", mailbox, fileName);
                    mail.Load(new PropertySet(ItemSchema.MimeContent));

                    MimeContent mc = mail.MimeContent;
                    FileStream oFileStream = new FileStream(filePath, FileMode.Create);

                    oFileStream.Write(mc.Content, 0, mc.Content.Length);
                    oFileStream.Close();


    Regards urvinit

    • Marked as answer by urvinit Tuesday, February 19, 2013 5:31 AM
    Tuesday, February 19, 2013 5:30 AM
  • With Exchange Web Services is possible to save mails in both formats:

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

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

    • Proposed as answer by WPF8888 Wednesday, February 20, 2013 12:39 PM
    Wednesday, February 20, 2013 12:38 PM