我試圖將圖像嵌入到Outlook電子郵件中。基本上,我想每天爲生日祝福創建一個Windows服務。服務將向所有員工發送生日祝福。對於每一天,圖像模板將會不同,並且每天都會有背景顏色。使用C#和Asp.Net在郵件正文中嵌入圖像
我嘗試使用的代碼片段中this聯繫,但有兩個問題,面臨着:
- 當我添加圖片到HTML正文,郵件談到與紅十字會的前景(「X」)標記。我已經驗證了路徑,這方面沒有問題。
- 當我向HTML Body添加任何附加內容時,圖片將被替換爲該圖片。不知道我哪裏錯了。
我附上我試過至今的C#代碼:
private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
{
string path = Server.MapPath("~/Images/TestLogo.png");
Configuration config = System.Web.Configuration.WebConfigurationManager
.OpenWebConfiguration("~/");
var settings = (System.Net.Configuration.MailSettingsSectionGroup)
config.GetSectionGroup("system.net/mailSettings");
var smtp = settings.Smtp;
System.Net.Configuration.SmtpNetworkElement network = smtp.Network;
var outlookApp = new Microsoft.Office.Interop.Outlook.Application();
var mailitem = (MailItem)outlookApp.CreateItem(OlItemType.olMailItem);
mailitem.To = network.TargetName;
mailitem.Subject = subject;
Microsoft.Office.Interop.Outlook.Attachment attachment = mailitem.Attachments.Add(path, OlAttachmentType.olEmbeddeditem, null, "Test Image");
string imageCid = "[email protected]";
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid);
mailitem.BodyFormat = OlBodyFormat.olFormatRichText;
mailitem.HTMLBody = String.Format("<body bgcolor='#E6E6FA'>Dear TestMan,<img [email protected]:{0}\"></body>", imageCid);
//mailitem.Body = body;
mailitem.Display(false);
mailitem.Send();
}
可能的複製[發送電子郵件內嵌圖像(http://stackoverflow.com/questions/18358534/send-inline-image-in-email) – Sparrow