【问题标题】:Sending inline attachments with ews使用 ews 发送内联附件
【发布时间】:2012-02-17 14:10:16
【问题描述】:
我正在使用 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 中查看的正确方法。
【问题讨论】:
标签:
.net
exchangewebservices
outlook-web-app
inline-images
【解决方案1】:
以下代码适用于我,我可以在 Outlook/OWA/Mobile 中看到内联附件。
步骤:
带有 contentid 占位符的 HTML 正文
用实际的附件 contentid 替换占位符
-
创建一个新附件并设置内联属性(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();