简体   繁体   English

Office 365 API-嵌入图像

[英]Office 365 API - Embed image

I am already successfully sending emails via the api. 我已经通过api成功发送了电子邮件。 I now need to try and embed an image to the footer of the email. 我现在需要尝试将图像嵌入到电子邮件的页脚中。 I am running a wpf c# app and have loaded the image to my system and set it as content build type so I can get a handle on it. 我正在运行wpf c#应用程序,并将图像加载到我的系统中并将其设置为内容构建类型,因此可以对其进行处理。 The api requires a string as the body. api需要字符串作为主体。 I have created an HTML email format via the stringbuilder class. 我已经通过stringbuilder类创建了HTML电子邮件格式。 I am using the following code to try and embed the image. 我正在使用以下代码尝试嵌入图像。

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();

The image appears on the email but as a dead link, red cross. 该图像显示在电子邮件中,但为无效链接,红叉。 I'm not sure if I have to attach the image first or maybe render out to base64? 我不确定是否必须先附加图像或将其渲染到base64? Any help would be gratefully accepted. 任何帮助将不胜感激。 Thanks Scott 谢谢斯科特

EDIT: Code for API 编辑:API的代码

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

EDIT Jason seems to be getting me along the right route. 编辑杰森(Jason)似乎使我走了正确的路。 But I read somewhere that it may need saving as a draft and then sending. 但是我在某处读到它可能需要另存为草稿然后发送。 My mail api code is as follows; 我的邮件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();

and the page content (as requested) 和页面内容(根据要求)

<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" />

Still no attachment. 仍然没有附件。 Thanks 谢谢

Yes, you have to attach the file, and be sure to set IsInline to true and the ContentId property to the same value you use in the HTML markup. 是的,您必须附加文件,并确保将IsInline设置为true ,并将ContentId属性设置为与HTML标记中使用的相同的值。 See this post for the raw REST equivalent: How do I send Email with inline Attachments . 请参阅此文章以获取等效的原始REST: 如何发送带有嵌入式附件的电子邮件

OK. 好。 I found the answer with the help of this stock overflow question. 我借助此股票溢出问题找到了答案。 Here 这里

The key to this is the 关键是

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

Seems a problem with the API at the moment. 目前,API似乎有问题。 Thanks for all your help on this. 感谢您在此方面的所有帮助。 Hopefully this will help others out. 希望这会帮助其他人。 Scott 斯科特

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

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