2
votes

I'm using EWS to send an email with inline attachement(s).

I'm use following code for it:

var attachment = attachments.AddFileAttachment(path);
attachment.ContentId = cid;
attachment.IsInline = true;
attachment.ContentType = "PNG/Image";

Html body of the message contains following fragment

<img src=""cid:{cid}""></img>

where {cid} is a value of cid field.

It works when I check emails with Outlook but in OWA image is not show in message body.

Please suggest me right way to send mail with inline image via EWS to view in OWA.

1
Hi, did you find a solution for that?Rajesh Mishra

1 Answers

2
votes

The below code works for me and I can see the inline attachment in Outlook/OWA/Mobile.

Steps:

  1. HTML body with placeholders for contentids

  2. Replace that placeholders with the actual attachment contentids

  3. Create a new attachment and set the properties inline (true) and contentid (actual contentid for the associated attachment)

        string attachment = "c:\\inlineattachment.png";
    
        // Create an e-mail message using the ExchangeService.
        EmailMessage message = new EmailMessage(ExchangeServiceObject);
    
        // Subject
        message.Subject = "Email with inline attachments";
    
        // Message body with place holder for contentid
        message.Body = "Email body with inline attachment </br> <img src=\"cid:{0}\">";
        message.Body.BodyType = BodyType.HTML;
    
        // Replace the place holder with contentid
        // Random GUID is used to avoid name collision for contentids 
        string newGuid = Guid.NewGuid().ToString();
        message.Body = string.Format(message.Body, newGuid);
    
        // Create a new attachment and add necessary properties to make it inline
        message.Attachments.AddFileAttachment(attachment);
        message.Attachments[message.Attachments.Count - 1].IsInline = true;
        message.Attachments[message.Attachments.Count - 1].ContentId = newGuid;
    
        // Add recipeint
        message.ToRecipients.Add("[email protected]");
    
        // Send the e-mail message and save a copy.
        message.SendAndSaveCopy();