2

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.

4

1 回答 1

2

下面的代码对我有用,我可以在 Outlook/OWA/Mobile 中看到内联附件。

脚步:

  1. 带有 contentids 占位符的 HTML 正文

  2. 用实际的附件 contentids 替换占位符

  3. 创建一个新附件并设置属性 inline (true) 和 contentid(关联附件的实际 contentid)

        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();
    
于 2015-06-29T16:13:26.283 回答