1
votes

I'm using EWS to read and extract images from an email served by exchange 2013. using the code below which is working great when the images are attached as actual attachments.. The problem arises when images come through as inline attachments.

The EWS .Hasattachments does not return true for inline attachments.. It seems silly that this has not been thought of.. Reading the article below there appears to be a work around but I just wondering what is the best, most efficient way to retrieve both regular & inline image attachments and save them to a directory.

https://social.technet.microsoft.com/Forums/office/en-US/ad10283a-ea04-4b15-b20a-40cbd9c95b57/exchange-2010-ews-c-display-html-email-problem-with-image?forum=exchangesvrdevelopment

  if (mail.HasAttachments && mail.Attachments[0] is FileAttachment)
                        {
                            int count = 0;
                            foreach (Attachment attachment in mail.Attachments)
                            {
                                if (attachment is FileAttachment && attachment.ContentType == "image/jpeg")
                                {
                                    Console.WriteLine(attachment.Name);
                                    FileAttachment fileAttachment = attachment as FileAttachment;
                                    string imagename = s + "-" + System.DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";

                                    /* download attachment to folder */
                                    fileAttachment.Load(imageLocation + "\\Images\\" + imagename);

}

1
I changed my code to below to but It just throws an error every time on the .load statement of Error itemNot Found.user552769
Console.WriteLine(attachment.Name); string sID = attachment.ContentId; sType = sType.Replace("image/", ""); string sFilename = attachment.Name; string sPathPlusFilename = Directory.GetCurrentDirectory() + "\\" + sFilename; ((FileAttachment)attachment).Load(sFilename);user552769

1 Answers

2
votes

It looks like you got some inspiration from this MSDN article in your first line. You shouldn't need to check HasAttachments but just iterate through the mail.Attachments themselves.

foreach (var attachment in mail.Attachments.Where(a => a is FileAttachment && a.ContentType == "image/jpeg"))
{
    Console.WriteLine(attachment.Name);
    FileAttachment fileAttachment = attachment as FileAttachment;
    string imagename = s + "-" + System.DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";

    /* download attachment to folder */
    fileAttachment.Load(imageLocation + "\\Images\\" + imagename);
}

I haven't tested the above but that should only do work on any jpeg FileAttachment