我正在使用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中查看。
下面的代码对我有效,我可以在Outlook/OWA/Mobile中看到内联附件。
步骤:
-
带内容占位符的HTML正文
-
将占位符替换为实际的附件内容ID
-
创建一个新的附件,并将属性设置为内联(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();