none
Saving an item attachment as .msg or .eml file. RRS feed

  • Question

  • How do I save an message or mail item as .eml or .msg file using EWS managed API

    Thanks Karthik G

    Thursday, October 25, 2012 8:53 AM

Answers

  • You can get the MimeContent of the Item Attachment and save it as .mht file.

           

    publicstaticList<MailAttachment> TraverseItemAttachment(Microsoft.Exchange.WebServices.Data.Attachmentattachment)

            {

    List<MailAttachment> mailattchmentList = newList<MailAttachment>();

        

    if(attachment isMicrosoft.Exchange.WebServices.Data.FileAttachment)

                {

                    Microsoft.Exchange.WebServices.Data.

    FileAttachmentfileAttachment = attachment asMicrosoft.Exchange.WebServices.Data.FileAttachment;

                    fileAttachment.Load();

                   

    MailAttachmentmailattchment = newMailAttachment();

                    mailattchment.AttachmentName = fileAttachment.Name;

                    mailattchment.AttachmentContent = fileAttachment.Content;

                    mailattchment.FileType = fileAttachment.ContentType;

                    mailattchmentList.Add(mailattchment);

                }

               

    elseif(attachment isMicrosoft.Exchange.WebServices.Data.ItemAttachment)

                {

                    Microsoft.Exchange.WebServices.Data.

    ItemAttachmentitemAttachment = attachment asMicrosoft.Exchange.WebServices.Data.ItemAttachment;

                    itemAttachment.Load(

    newPropertySet(ItemSchema.Attachments));

                    itemAttachment.Load(

    newPropertySet(ItemSchema.MimeContent));

                   

    if(itemAttachment.Item isEmailMessage)

                    {

                       

    EmailMessageattachMessage = itemAttachment.Item asEmailMessage;

                       

    MailAttachmentmailattchment = newMailAttachment();

                        mailattchment.AttachmentName = attachMessage.Subject +

    ".mht";

                        mailattchment.AttachmentContent = attachMessage.MimeContent.Content;

                        mailattchment.FileType =

    "application/octet-stream";

                        mailattchmentList.Add(mailattchment);

                       

    if(attachMessage.HasAttachments)

                        {

                           

    foreach(Microsoft.Exchange.WebServices.Data.AttachmentsubAttachment inattachMessage.Attachments)

                            {

                               

    foreach(MailAttachmentsubEntry inTraverseItemAttachment(subAttachment))

                                {

                                    mailattchmentList.Add(subEntry);

                                }

                            }

                        }

                    }

                }

               

    returnmailattchmentList;

            }



    Thanks Karthik G

    • Marked as answer by G_Karthik Friday, February 1, 2013 11:06 AM
    Friday, February 1, 2013 11:06 AM

All replies

  • PST and MSG are both Office(Outlook) file formats (eg a msg file is a compound OLE document) so to export a mailbox to a PST file you need to use MAPI either extended Mapi or you could use the OOM or possibly a third party library like Redemption http://www.dimastr.com/redemption/home.htm

    You can use EWS to export to EML using the MIME content with a Item attachment however you can only export the Root Mail Item not the attachment. If the ItemAttachment is anything other then a message (eg contact,task) etc then using EML isn't a good idea because it won't include the Mapi properties that represent the message. It maybe better to try and explain you reasons for needing to do this.

    Cheers
    Glen

     
    Friday, October 26, 2012 3:05 AM
  • I have a requirement where I need to save all the attachments in a mail message. So I used the EWS managed API to get the attachments and I was able to save all the files with their appropriate format (Such as doc, ppt, xls etc...) But if the mail message has another mail message as attachment, I am not able to save as an eml. I tired with the MIME content, but it is opening as text file but the attachments in the inner message are not opened.


    Thanks Karthik G

    Friday, October 26, 2012 5:05 AM
  • > I  have a requirement where I need to save all the attachments in a mail message

      The next question is why do you need to do this ? eg what are going to do with the attachments ?

      The reason i ask this is for Item attachments eg Contact, Tasks, Notes etc the only file format that would allow you to open this item in Outlook (which would be the only application where they would make any sense) is the MSG format (EML is just a MIME stream and only valid for Email not other rich exchange types). As I've already mentioned MSG is the OLE compound file format associated with Microsoft Office so if you want to use this file format you will need to use Office or OLE API eg Outlook OOM, Redemption http://www.dimastr.com/redemption/home.htm , http://www.independentsoft.com/msg/index.html, or anything the wrappers IStorage or supports compound storage http://openmcdf.sourceforge.net/. The problem with this  is you still can't get full fidelity on Item Attachments using EWS in 2010 or 2007 so you need to use MAPI (eg the OOM or Redemption or Exmapi) if this is a hard requirement. 

    Cheers
    Glen

    Monday, October 29, 2012 2:55 AM
  • Hi Glen Scales,

                 Is it possible to save an ItemAttachment???For FileAttachment we using the following EWS Managed API Code to save,

      if(attachment is FileAttachment)
    {
      FileAttachment fAttachment = new FileAttachment();
      fAttachment.Load("D:\\Stream" + fAttachment.Name);
    }

              What about For ItemAttachment???How can we save the ItemAttachment like this in a specified folder???


    • Edited by Vamsidaya Friday, February 1, 2013 11:50 AM
    Friday, February 1, 2013 11:01 AM
  • You can get the MimeContent of the Item Attachment and save it as .mht file.

           

    publicstaticList<MailAttachment> TraverseItemAttachment(Microsoft.Exchange.WebServices.Data.Attachmentattachment)

            {

    List<MailAttachment> mailattchmentList = newList<MailAttachment>();

        

    if(attachment isMicrosoft.Exchange.WebServices.Data.FileAttachment)

                {

                    Microsoft.Exchange.WebServices.Data.

    FileAttachmentfileAttachment = attachment asMicrosoft.Exchange.WebServices.Data.FileAttachment;

                    fileAttachment.Load();

                   

    MailAttachmentmailattchment = newMailAttachment();

                    mailattchment.AttachmentName = fileAttachment.Name;

                    mailattchment.AttachmentContent = fileAttachment.Content;

                    mailattchment.FileType = fileAttachment.ContentType;

                    mailattchmentList.Add(mailattchment);

                }

               

    elseif(attachment isMicrosoft.Exchange.WebServices.Data.ItemAttachment)

                {

                    Microsoft.Exchange.WebServices.Data.

    ItemAttachmentitemAttachment = attachment asMicrosoft.Exchange.WebServices.Data.ItemAttachment;

                    itemAttachment.Load(

    newPropertySet(ItemSchema.Attachments));

                    itemAttachment.Load(

    newPropertySet(ItemSchema.MimeContent));

                   

    if(itemAttachment.Item isEmailMessage)

                    {

                       

    EmailMessageattachMessage = itemAttachment.Item asEmailMessage;

                       

    MailAttachmentmailattchment = newMailAttachment();

                        mailattchment.AttachmentName = attachMessage.Subject +

    ".mht";

                        mailattchment.AttachmentContent = attachMessage.MimeContent.Content;

                        mailattchment.FileType =

    "application/octet-stream";

                        mailattchmentList.Add(mailattchment);

                       

    if(attachMessage.HasAttachments)

                        {

                           

    foreach(Microsoft.Exchange.WebServices.Data.AttachmentsubAttachment inattachMessage.Attachments)

                            {

                               

    foreach(MailAttachmentsubEntry inTraverseItemAttachment(subAttachment))

                                {

                                    mailattchmentList.Add(subEntry);

                                }

                            }

                        }

                    }

                }

               

    returnmailattchmentList;

            }



    Thanks Karthik G

    • Marked as answer by G_Karthik Friday, February 1, 2013 11:06 AM
    Friday, February 1, 2013 11:06 AM
  • save you file .eml or .msg
    Saturday, October 24, 2020 9:10 AM
  • The Betavare MSG to EML Exporter tool is a perfect tool because it has been good batch exportation. The software can export unlimited MSG files at the same time. The software exports all of your selected data. It's select your files by yourself. The software is very easy to use. You don’t have to learn any kind of technical assistance. It takes no time to learn how it works. 
    Steps:- 
    1. To export MSG to EML file format, click on the add files button and add your MSG folder. 
    2. You can view all MSG files, browse the output location, and click on the export button on save EML files. 

    Visit here:-  https://www.betavare.com/msg/eml/
    Wednesday, November 25, 2020 8:00 AM