我已经通过 api 成功发送电子邮件。我现在需要尝试将图像嵌入到电子邮件的页脚。我正在运行一个 wpf c# 应用程序并将图像加载到我的系统并将其设置为内容构建类型,以便我可以处理它。api 需要一个字符串作为主体。我通过 stringbuilder 类创建了一个 HTML 电子邮件格式。我正在使用以下代码尝试嵌入图像。
sb.Append("<p style=\"text-align: left;\"> </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);
编辑杰森似乎让我走上了正确的道路。但我在某处读到它可能需要保存为草稿然后发送。我的邮件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; </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 - 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 +44 (0) 111 111 111 – H24<br>s xxx<br>e <a href="mailto:xxx">xxx</a></p><p style="text-align: left;"> </p><img src="cid:my_inline_attachment" />
还是没有附件。谢谢