使用ews发送内联附件

时间:2012-01-24 10:09:18

标签: .net exchangewebservices outlook-web-app inline-images

我正在使用EWS发送带有内联附件的电子邮件。

我正在使用以下代码:

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

消息的Html正文包含以下片段

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

其中{cid}是 cid 字段的值。

当我使用Outlook检查电子邮件但OWA图像未在邮件正文中显示时,它可以正常工作。

请建议我通过EWS发送包含内联图片的邮件的正确方式,以便在OWA中查看。

1 个答案:

答案 0 :(得分:2)

以下代码适用于我,我可以在Outlook / OWA / Mobile中看到内联附件。

步骤:

  1. 带有占位符的HTML正文

  2. 将这些占位符替换为实际附件contentids

  3. 创建新附件并将属性设置为内联(true)和 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();