none
In Line Attachments RRS feed

  • Question

  • Hi ,

     

    I am using Exchange Web Service to capture emails for a email account. I am able to download the attachments of the mail and the body text.

    Can someone help me in downloading the Inline Attachments of the mail. All the attachments(Images) that are attached in the body of the mail.

     

    • Moved by John Saunders Monday, July 18, 2011 7:06 PM Exchange web service question (From:ASMX Web Services and XML Serialization)
    Monday, July 18, 2011 6:21 PM

All replies

  • All the attachments that are attached to a message whether they are inline, normal or contact picture etc will all be returned via EWS when you use a GetAttachment operation or using something like this with the Managed API

     

              EmailMessage email = EmailMessage.Bind(service,"Itemid");
              foreach (Attachment at in itItem.Attachments) {
                if (at.IsInline == true) { 
                
                }
              }
    

    So if availble the Inline Images should be there and you should be able to find the ContentID and then referance this back to the Message bodies ContentID. The thing to be carefull of is that there maybe just images linked in the body of the message rather than be an inline attachment. For this you need to look at the struture of the HTML body.

    Cheers
    Glen

    Tuesday, July 19, 2011 3:20 AM
  • I had them display the inline image in mail by the following way.

    EmailMessage message = EmailMessage.Bind(this.exchangeService, id);
     
    string sHTMLCOntent = message.Body.Text;
    FileAttachment[] attachments = null;
     
    if (message.Attachments.Count != 0)
    {
        attachments = new FileAttachment[message.Attachments.Count];
        for (int i = 0; i < message.Attachments.Count; i++)
        {
            try
            {
                if (message.Attachments[i].IsInline)
                {
                    string sType = message.Attachments[i].ContentType.ToLower();
                    if (sType.Contains("image"))
                    {
                        attachments[i] = (FileAttachment)message.Attachments[i];
                        string sID = attachments[i].ContentId;
                        sType = sType.Replace("image/""");
                        string sFilename = sID + "." + sType;
                        string sPathPlusFilename = Directory.GetCurrentDirectory() + "\\" + sFilename;
                        attachments[i].Load(sFilename);
                        string oldString = "cid:" + sID;
                        sHTMLCOntent = sHTMLCOntent.Replace(oldString, sPathPlusFilename);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            mail.Body = sHTMLCOntent;
        }
    }
    Wednesday, July 20, 2011 7:07 AM
  • Hi, see my approach here

    http://exchws.wordpress.com/2011/06/29/get-attachments-from-a-message/


    MVP Office Development, MCP, MCTS SharePoint 2010 Development
    Thursday, July 21, 2011 11:22 AM
  • Upss, sorry, this one

    http://exchws.wordpress.com/2011/07/21/read-message-body-with-inline-attachments-and-render-it/


    MVP Office Development, MCP, MCTS SharePoint 2010 Development
    Thursday, July 21, 2011 1:17 PM
  • Hi,

    To get the attachments including the inline pictures from mailbox, you need to use GetAttachmentType class. GetItemType Class will not retrieve the content of the attachments. More detail information, please refer the following codes, especially the codes in red (It retrieves all  the attachments in a user's mailbox and save them in local drive). You can download the sample (Readmex) from my skydrive:

    https://skydrive.live.com/?cid=2d68430fc8d84837&sc=documents&uc=1&id=2D68430FC8D84837%21182#

    -------------------------------------------------------------

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using ReadMex.com.fujian.ex14;

    using System.IO

    namespace ReadMex

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                const string sUrl = "https://ex14.fujian.com/EWS/Exchange.asmx";     //Exchange  WebService URI

     

                const string sUsr = "administrator";                             //Username for the user that has service account level

     

                const string sPwd = "0okM9ijn";                         // Password

     

                const string sDom = "fujian.com";                         //Your Exchange Domain Name

                ExchangeServiceBinding esb = new ExchangeServiceBinding();

     

                esb.AllowAutoRedirect = true;

     

                esb.Url = sUrl;

     

                esb.Credentials = new System.Net.NetworkCredential(sUsr, sPwd, sDom);

     

                //form the findItem request

                FindItemType fir = new FindItemType();

                fir.Traversal = ItemQueryTraversalType.Shallow;

     

                //define which item properties are returned in the response.

                ItemResponseShapeType itemProperties = new ItemResponseShapeType();

                itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;

     

                //add properties shape to the request.

     

                fir.ItemShape = itemProperties;

     

                /// identify which folders to search to find items

     

                DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[2];

                folderIDArray[0] = new DistinguishedFolderIdType();

                folderIDArray[1] = new DistinguishedFolderIdType();

                folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;

                folderIDArray[1].Id = DistinguishedFolderIdNameType.drafts;

     

                //add folders to the request.

     

                fir.ParentFolderIds = folderIDArray;

     

                try

                {

     

                    FindItemResponseType findItemResponse = esb.FindItem(fir);

                  

                  

     

     

                    //get the responese messages

                    ResponseMessageType[] rmta = findItemResponse.ResponseMessages.Items;

                    foreach (FindItemResponseMessageType responseMessage in findItemResponse.ResponseMessages.Items)

                    {

                     

                        if (responseMessage.ResponseClass == ResponseClassType.Success) {

     

                            ArrayOfRealItemsType mailboxItems = (ArrayOfRealItemsType)responseMessage.RootFolder.Item;

                            for (int itemCount = 0; itemCount < mailboxItems.Items.Length; itemCount++)

                            {

                                //textBox2.Text = mailboxItems.Items.Length.ToString();

     

                                ItemType inboxitem = mailboxItems.Items[itemCount];

                                GetItemType getItemRequest = new GetItemType();

                                getItemRequest.ItemShape = new ItemResponseShapeType();

                                getItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;

                               

                                //Convernt the message body from HTML to text

                                //getItemRequest.ItemShape.BodyType = BodyTypeResponseType.Text;

                                //getItemRequest.ItemShape.BodyTypeSpecified = true;

     

                                getItemRequest.ItemIds = new ItemIdType[1];

                                getItemRequest.ItemIds[0] = (BaseItemIdType)inboxitem.ItemId;

                               

                                GetItemResponseType getItemresponse = esb.GetItem(getItemRequest);

                                ItemInfoResponseMessageType getItemResponseMessage = getItemresponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;

     

                                if (getItemResponseMessage != null)

                                {

                               

                                if(getItemResponseMessage.ResponseClass==ResponseClassType.Success&& getItemResponseMessage.Items.Items!=null &&getItemResponseMessage.Items.Items.Length>0)

                                {

                                    inboxitem = getItemResponseMessage.Items.Items[0];

                               

                                }

                                                   

                                }

                                textBox1.Text += "Message Subject: " + inboxitem.Subject + "\r\n\r\n" + "Recipients: " + inboxitem.DisplayTo + "\r\n\r\n" + "Attachment items:"  + inboxitem.Attachments.Length;

                                textBox1.Text += "\r\n\r\n*********************\r\n\r\n";

     

                                AttachmentType[] attachments = inboxitem.Attachments ;

                                for (int attachmentcount = 0; attachmentcount < attachments.Length; attachmentcount++)

                                {

                                    GetAttachmentType getAttachmentrequest = new GetAttachmentType();

                                    RequestAttachmentIdType[] attachmentidArray = new RequestAttachmentIdType[1];

                                    attachmentidArray[0] = new RequestAttachmentIdType();

                                    attachmentidArray[0].Id = attachments[attachmentcount].AttachmentId.Id;

                                    getAttachmentrequest.AttachmentIds = attachmentidArray;

                                    GetAttachmentResponseType getAttachmentresponse = esb.GetAttachment(getAttachmentrequest);

                                    AttachmentInfoResponseMessageType attachmentresponesemessage = getAttachmentresponse.ResponseMessages.Items[0] as AttachmentInfoResponseMessageType;

                                    textBox2.Text += attachmentresponesemessage.Attachments[0].Name;

                                    textBox2.Text += "\r\n\r\n";

                                    FileAttachmentType thefileattachment = (FileAttachmentType)attachmentresponesemessage.Attachments[0];

                                    using (Stream FiletToDisk = new FileStream(@"c:\Images\" + itemCount + attachmentresponesemessage.Attachments[0].Name, FileMode.Create))

                                    {

     

                                        FiletToDisk.Write(thefileattachment.Content, 0, thefileattachment.Content.Length);

                                        FiletToDisk.Flush();

                                        FiletToDisk.Close();

     

                                    }

                                }

                             

                            }

                        }

     

                 

                       

                    }

                }

                   

     

                catch (Exception ve) {

                    Console.WriteLine(ve.Message);

                }

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

     

            }

        }

    }
    }

     


    Tuesday, July 26, 2011 8:02 AM
    Moderator
  • Hi!

    You don't need to store the InLine images into HDD, just convert it and render it, see my post

    Convert.ToBase64String(attachments[i].Content);
    



    MVP Office Development, MCP, MCTS SharePoint 2010 Development
    Tuesday, July 26, 2011 1:41 PM
  • Hi,

    Yes, it is not necessary to download the image before rendering them.


    Thursday, July 28, 2011 7:12 AM
    Moderator