Reply email with inline images EWS using C#

NILESH PATEL 21 Reputation points
2022-01-21T12:49:01.197+00:00

I am trying to reply an email with inline images in email body. But I did not get any success.
My code is as below

                            PropertySet propSet = new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.LastModifiedTime);
                            EmailMessage _messageReply = EmailMessage.Bind(service, outlookEmailId, propSet); //binding with existing incoming email , outLookEmailId = UniqueId of incoming email
                           ResponseMessage responseMessage = _messageReply.CreateReply(true);                              
                           responseMessage.BodyPrefix = Body; // html body contains inline images with base64 file system i.e. <img src="data:image/png;base64,iVBORw0KGgoAA"/>
                           _messageReply= responseMessage.Save();
                           _messageReply.SendAndSaveCopy();

After SendAndSaveCopy, at recipient end, it does not show any inline images.
For new email it works perfect , problem is with replying email ( using Bind method)
Does anybody know, how to reply an email with inline images?

Exchange | Exchange Server | Development
Developer technologies | C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-01-24T02:24:51.113+00:00

    @NILESH PATEL , based on my test, I could use the following code to send a mail with inline message and you can have a look.

            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);  
            service.Credentials= new NetworkCredential("******@hotmail.com","password");  
            service.Url=new Uri("https://outlook.office365.com/EWS/Exchange.asmx");  
            ItemView view = new ItemView(50);  
            List<SearchFilter> searchFilterCollection = new List<SearchFilter>();  
            searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Test0220"));  
            SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection.ToArray());  
            FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);  
            PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, EmailMessageSchema.IsRead);  
            EmailMessage message = EmailMessage.Bind(service, results.FirstOrDefault().Id, propSet);  
    
            ResponseMessage responseMessage = message.CreateReply(true);  
    
            string file = @"D:\2.jpg";  
            message.Attachments.AddFileAttachment("2.jpg", file);  
            message.Attachments[0].IsInline = true;  
            message.Attachments[0].ContentId = "2.jpg";  
            responseMessage.BodyPrefix = new MessageBody(BodyType.HTML, @"<img width=100 height=100 id=""1"" src=""cid:2.jpg"">");  
            responseMessage.Save();  
            responseMessage.Send();  
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Sreeju Nair 12,666 Reputation points
    2022-01-22T16:40:05.64+00:00

    Most Email Clients are not supporting Base 64 Images in the Body, these are part of their security measures though a large amount of criticism out there.

    One of the alternative is to use Content-ID Format: https://www.rfc-editor.org/rfc/rfc2392.html

    An example is available there.

    https://www.c-sharpcorner.com/UploadFile/40e97e/html-email-with-embedded-image/


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.