asp.net-mvc - 使用 Microsoft Graph 或 Outlook REST API 在电子邮件正文中呈现嵌入图像

标签 asp.net-mvc email outlook office365 microsoft-graph-api

当我们使用 Microsoft Graph/Outlook REST API 收到电子邮件时,它的正文包含对嵌入图像的引用,如下所示。

<img src="cid:image001.jpg@1D3E60C.5A00BC30">

我正在寻找一种方法,以便我可以正确显示嵌入的图像,因为上面的图像标签不显示任何图像。我做了一些搜索,但没有找到任何帮助。

以下是使用 Microsoft Graph API 通过 id 获取电子邮件的示例代码。
// Get the message.
Message message = await graphClient.Me.Messages[id].Request(requestOptions).WithUserAccount(ClaimsPrincipal.Current.ToGraphUserAccount()).GetAsync();

最佳答案

要使用 Microsoft Graph API 通过电子邮件获取附加资源,您需要收到如下电子邮件。

// Get the message with all attachments(Embedded or separately attached).
Message message = await graphClient.Me.Messages[id].Request(requestOptions).WithUserAccount(ClaimsPrincipal.Current.ToGraphUserAccount()).Expand("attachments").GetAsync();

一旦您拥有带有电子邮件详细信息的所有附件,您需要遍历附件列表并检查附件 IsInline 属性是否设置为 true,然后只需替换

cid:image001.jpg@1D3E60C.5A00BC30



使用从附件的字节数组创建的 Base64String。
string emailBody = message.Body.Content;
foreach (var attachment in message.Attachments)
{
   if (attachment.IsInline.HasValue && attachment.IsInline.Value)
   {
     if ((attachment is FileAttachment) &&(attachment.ContentType.Contains("image")))
     {
        FileAttachment fileAttachment = attachment as FileAttachment;
        byte[] contentBytes = fileAttachment.ContentBytes;
        string imageContentIDToReplace = "cid:" + fileAttachment.ContentId;
        emailBody = emailBody.Replace(imageContentIDToReplace, 
        String.Format("data:image;base64,{0}", Convert.ToBase64String(contentBytes as 
        byte[])));
     }

  }
}

现在使用 emailBody 变量渲染电子邮件正文,它将显示所有嵌入的图像。

关于asp.net-mvc - 使用 Microsoft Graph 或 Outlook REST API 在电子邮件正文中呈现嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50879626/

相关文章:

email - 将iCalendar事件附加到Grails中的邮件

c++ - 从 C++ 通过 activex 读取 Outlook 地址簿随机返回未知错误

c# - 使用 Fluent Validation 进行条件验证

asp.net-mvc - 在 MVC 中定义 html5 数据属性

javascript - 我应该在 ASP.NET MVC 应用程序中的哪里引用 Angular Controller ?

ruby-on-rails - RSpec 发送电子邮件(不存储在 ActionMailer::Base.deliveries 中) - 不知道为什么?

javascript - Bootstrap 模式无法关闭

email - 从visualsvn向office365提交后提交电子邮件

javascript - 文件夹主页中的用户控件未初始化

asp.net - 如何创建具有多个 VEVENT 的 .ICS 文件以导入到现有 Outlook 日历中