簡體   English   中英

Office 365 API-嵌入圖像

[英]Office 365 API - Embed image

我已經通過api成功發送了電子郵件。 我現在需要嘗試將圖像嵌入到電子郵件的頁腳中。 我正在運行wpf c#應用程序,並將圖像加載到我的系統中並將其設置為內容構建類型,因此可以對其進行處理。 api需要字符串作為主體。 我已經通過stringbuilder類創建了HTML電子郵件格式。 我正在使用以下代碼嘗試嵌入圖像。

sb.Append("<p style=\"text-align: left;\">&nbsp;</p>");

        var avHtml = AlternateView.CreateAlternateViewFromString(sb.ToString(), null, MediaTypeNames.Text.Html);
        string path = Environment.CurrentDirectory + @"\images\fordEmail.jpg";
        var inline = new LinkedResource(path, MediaTypeNames.Image.Jpeg);
        inline.ContentId = Guid.NewGuid().ToString();
        avHtml.LinkedResources.Add(inline);

        sb.Append(String.Format(@"<img src=""cid:{0}"" />", inline.ContentId));


        return sb.ToString();

該圖像顯示在電子郵件中,但為無效鏈接,紅叉。 我不確定是否必須先附加圖像或將其渲染到base64? 任何幫助將不勝感激。 謝謝斯科特

編輯:API的代碼

mail.Subject = subject;
mail.Body = new ItemBody() { Content = body, ContentType = BodyType.HTML };
await client.Me.SendMailAsync(mail, true);

編輯傑森(Jason)似乎使我走了正確的路。 但是我在某處讀到它可能需要另存為草稿然后發送。 我的郵件api代碼如下;

mail.Subject = subject;
            mail.Body = new ItemBody() { Content = body, ContentType = BodyType.HTML };
            await client.Me.Messages.AddMessageAsync(mail);
            var messageId = mail.Id;
            string path = Environment.CurrentDirectory + @"\images\fordEmail.jpg";
            Image img = Image.FromFile(path);
            byte[] arr;
            using (var ms = new MemoryStream())
            {
                img.Save(ms, ImageFormat.Jpeg);
                arr = ms.ToArray();
            }

            mail.Attachments.Add(new FileAttachment()
            {
                IsInline = true,
                ContentBytes = arr,
                Name = "fordEmail.jpg",
                ContentId = "my_inline_attachment"
            });

            await client.Me.Messages[messageId].SendAsync();

和頁面內容(根據要求)

<p><strong>Automated message from xxx.</strong></p><p>*Amendment from previous notification</p><style type="text/css">.tg  {border-collapse:collapse;border-spacing:0;border-color:#aabcfe;}.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 50px 10px 10px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#669;background-color:#e8edff;border-top-width:1px;border-bottom-width:1px;}.tg th{font-family:Arial, sans-serif;font-size:14px;text-align-left;font-weight:normal;padding:10px 50px 10px 10px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#039;background-color:#b9c9fe;border-top-width:1px;border-bottom-width:1px;}p  {font-family:Arial, sans-serif;font-size:12px}p.padding {padding-right: 50px}p.smallFont {font-size:9px}</style><p>Flight xxx has now arrived. Please find the details below;&nbsp;</p><table class="tg"><tr><th class="tg-031e" colspan="2" text-alight:left>Flight Details</th></tr><tr><td class="tg-031e"<p>Date</p></td><td class="tg-031e"<p class="DecimalAligned">07/05/2015</p></td></tr><tr><td class="tg-031e"<p>Flight</p></td><td class="tg-031e"<p class="DecimalAligned">xxx469J</p></td></tr><tr><td class="tg-031e"<p>Route</p></td><td class="tg-031e"<p class="DecimalAligned">DUB -&nbsp;FNC</p></td></tr><tr><td class="tg-031e"<p class="padding">Scheduled / Actual Time Departure</p></td><td class="tg-031e"<p class="DecimalAligned">07:10 / 12:00 (UTC)</p></td></tr><tr><td class="tg-031e"<p>Scheduled / Actual Time Arrival</p></td><td class="tg-031e"<p class="DecimalAligned">10:55 / 14:00 (UTC)</p></td></tr><tr><td class="tg-031e"<p>TOB</p></td><td class="tg-031e"<p class="DecimalAligned">100+1</p></td></tr></tbody></table><p class="smallFont"><em>Source: xxx</em></span></p><p>Comments : TEST </p><p>Should you require any further information please do not hesitate to contact us </p><p>Operations Manager<br>xxx<br>t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+44 (0) 111 111 111 – H24<br>s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xxx<br>e&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="mailto:xxx">xxx</a></p><p style="text-align: left;">&nbsp;</p><img src="cid:my_inline_attachment" />

仍然沒有附件。 謝謝

是的,您必須附加文件,並確保將IsInline設置為true ,並將ContentId屬性設置為與HTML標記中使用的相同的值。 請參閱此文章以獲取等效的原始REST: 如何發送帶有嵌入式附件的電子郵件

好。 我借助此股票溢出問題找到了答案。 這里

關鍵是

// Update with attachments
await m.UpdateAsync();
// Send the message
await m.SendAsync();

目前,API似乎有問題。 感謝您在此方面的所有幫助。 希望這會幫助其他人。 斯科特

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM