Office 365 API - 嵌入图像

时间:2015-06-30 12:06:53

标签: c# office365 outlook-restapi

我已成功通过api发送电子邮件。我现在需要尝试将图像嵌入到电子邮件的页脚中。 我正在运行一个wpf c#app并将图像加载到我的系统并将其设置为内容构建类型,以便我可以处理它。 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? 任何帮助都将被感激地接受。 谢谢Scott

编辑: API代码

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

EDIT 杰森似乎让我沿着正确的路线前进。但我在某处读到它可能需要保存为草稿然后发送。 我的邮件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" />

仍然没有依恋。 感谢

2 个答案:

答案 0 :(得分:1)

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. See this post for the raw REST equivalent: How do I send Email with inline Attachments.

答案 1 :(得分:1)

行。我在这个股票溢出问题的帮助下找到了答案。 Here

关键是

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

目前似乎API存在问题。感谢您对此的所有帮助。希望这会帮助其他人。 斯科特