Sending inline attachments with ews

穿精又带淫゛_ 提交于 2019-12-06 11:47:34

问题


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:


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("recipient@domain.com");
    
        // Send the e-mail message and save a copy.
        message.SendAndSaveCopy();
    


来源:https://stackoverflow.com/questions/8984982/sending-inline-attachments-with-ews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!