Save mail to msg file using EWS API

后端 未结 8 2178

I\'m using Exchange Web Services Managed API 1.1 to connect to Exchange server 2010 and then find out new emails received. Now I want to save a copy of the .msg file to a fo

8条回答
  •  我在风中等你
    2020-11-28 10:32

    You can download all the attachments using EWS API and C# . Below is the example given:

    byte[][] btAttachments = new byte[3][]; //To store  3 attachment 
    
    if (item.HasAttachments) {
        EmailMessage message = EmailMessage.Bind(objService, new ItemId(item.Id.UniqueId.ToString()), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
    
        noOfAttachment = message.Attachments.Count;
    
        // Iterate through the attachments collection and load each attachment.
        foreach(Attachment attachment in message.Attachments)
        {
            if (attachment is FileAttachment)
            {
                FileAttachment fileAttachment = attachment as FileAttachment;
                // Load the file attachment into memory and print out its file name.
                fileAttachment.Load();
                //Get the Attachment as bytes
                if (i < 3) {
                    btAttachments[i] = fileAttachment.Content;
                    i++;
                }
            }
            // Attachment is an item attachment.
                        else
            {
                // Load attachment into memory and write out the subject.
                ItemAttachment itemAttachment = attachment as ItemAttachment;
                itemAttachment.Load(new PropertySet(EmailMessageSchema.MimeContent));
                MimeContent mc = itemAttachment.Item.MimeContent;
                if (i < 3) {
    
                    btAttachments[i] = mc.Content;
                    i++;
                }
            }
        }
    }
    

    Above code converts all the attachment into bytes. Once you have bytes, you can convert bytes into your required format. To Convert bytes into files and save in the disk follow the below links: Write bytes to file http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Save-byte-array-to-file.html

提交回复
热议问题