繁体   English   中英

如何使用 Microsoft Graph 在 Email Body 中嵌入图像

[英]How to embed Image inline in Email Body using Microsoft Graph

我正在使用 Function 应用程序触发邮件,使用 MS Graph API,邮件正文文本被正确触发,但在渲染 header 和页脚图像时遇到问题。 如何在身体层面解决这个问题。

在此处输入图像描述

以下是上述图片在 HTML/Blob 文件中的引用

  <img src=cid:Header.jpg>
    <img src=cid:footer.png>
    <ContentIDs>Header.jpg, footer.png</ContentIDs>

用于渲染主体的代码。

             var mailContent = new Message
                {
                    Subject = em.Subject,
                    Body = new ItemBody
                    {
                        ContentType = BodyType.Html,
                        Content = m.Body,
                        ODataType = null
                    },
                    ToRecipients = toEmails,
                    CcRecipients = ccEmails,
                    ODataType = null
                };    

编辑:目前在 Function 应用程序中面临此更改后的错误请求。 我正在努力解决这个问题。 如果您在下面的代码中发现任何差异,请随时发表评论。

            var imagePath = @"<path\Header.jpg>";
            var imageID = "Header.jpg";//file name
            byte[] imageArray = System.IO.File.ReadAllBytes(imagePath);
            var imagePath2 = @"<path\footer.png">;
            var imageID2 = "footer.png";
            byte[] imageArray2 =System.IO.File.ReadAllBytes(imagePath2);

            
            var mContent = new Message
            {
                Subject = t.Subject,//parsing from the template
                Body = new ItemBody
                {
                    ContentType = BodyType.Html,
                    Content = m.Body,
                    ODataType = "#microsoft.graph.fileAttachment"
                },
                ToRecipients = toEmails,
                CcRecipients = ccEmails,
                ODataType = "#microsoft.graph.fileAttachment",
                HasAttachments = true,
                Attachments = new MessageAttachmentsCollectionPage()
                    {
                            new FileAttachment
                        {
                                
                                ContentBytes= imageArray,
                                ContentType = "image/jpeg",
                                ContentId= imageID,
                                IsInline=true,
                                Name = "theHead",
                               
                        },
                            new FileAttachment
                            {
                                
                                ContentBytes= imageArray2,
                                ContentType = "image/png",
                                ContentId= imageID2,
                                IsInline=true,
                                Name = "thefoot",
                            }
                    }
            };

我为你写了一个演示,试试下面的简单控制台应用程序:

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;

namespace sendEmails
{
    class Program
    {
        static void Main(string[] args)
        {
            var appID = "";
            var appSec = "";
            var tenantID = "";
            

            IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                .Create(appID)
                .WithTenantId(tenantID)
                .WithClientSecret(appSec)
                .Build();

            ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);

            GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider);

            var imagePath = @"<your image path>";
            var imageID = "image1";
            

            byte[] imageArray = System.IO.File.ReadAllBytes(imagePath);

            var body = "<h1>this is superman </br> <img src='cid:"+ imageID + "'/></h1>";
            var attachments = new MessageAttachmentsCollectionPage()
            {
                new FileAttachment{
                    ContentType= "image/jpeg",
                    ContentBytes = imageArray,
                    ContentId = imageID,
                    Name= "test-image"
                }
            };
            
            var message = new Message
            {
                Subject = "TEST SENDING IMAGE ",
                Body = new ItemBody
                {
                    ContentType = BodyType.Html,
                    Content = body,
                    ODataType = null
                },
                ToRecipients = new List<Recipient>()
                {
                    new Recipient
                    {
                        EmailAddress = new EmailAddress
                        {
                            Address = "<receiver email>"
                        }
                    }
                },
                Attachments = attachments

            };
            
            graphServiceClient.Users["<user upn>"].SendMail(message, false).Request().PostAsync().GetAwaiter().GetResult();
            Console.WriteLine("ok");
        }
    }
}

结果:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM