2016-02-14 81 views
0

我試圖將圖像嵌入到Outlook電子郵件中。基本上,我想每天爲生日祝福創建一個Windows服務。服務將向所有員工發送生日祝福。對於每一天,圖像模板將會不同,並且每天都會有背景顏色。使用C#和Asp.Net在郵件正文中嵌入圖像

我嘗試使用的代碼片段中this聯繫,但有兩個問題,面臨着:

  1. 當我添加圖片到HTML正文,郵件談到與紅十字會的前景(「X」)標記。我已經驗證了路徑,這方面沒有問題。
  2. 當我向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(); 

    } 
+0

可能的複製[發送電子郵件內嵌圖像(http://stackoverflow.com/questions/18358534/send-inline-image-in-email) – Sparrow

回答

0
  1. 轉換圖像爲base64。或者與網站/工具等https://www.base64-image.de/或編程http://www.c-sharpcorner.com/blogs/convert-an-image-to-base64-string-and-base64-string-to-image

  2. 嵌入它:<img src="data:image/jpeg;base64,IMAGEDATA" />

    編輯的數據類型與轉換成基64字符串中的圖像字節匹配圖像的類型和替換「圖象 - 數據」 。的

+0

它沒有工作了。然而,我得到它解決了另一個類似的問題在另一個鏈接在堆棧溢出本身[http://stackoverflow.com/questions/18358534/send-inline-image-in-email] –

相關問題