简体   繁体   中英

Office 365 API - Embed image

I am already successfully sending emails via the 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. The api requires a string as the body. I have created an HTML email format via the stringbuilder class. 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? Any help would be gratefully accepted. Thanks Scott

EDIT: Code for 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. But I read somewhere that it may need saving as a draft and then sending. My mail api code is as follows;

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

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. Thanks for all your help on this. Hopefully this will help others out. Scott

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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