2013-05-09 71 views
0

我嘗試使用嵌入式圖像發送電子郵件。圖片從List<Bitmap>,我相信他們有100%。電子郵件中沒有嵌入式圖像

不知怎的,當我得到的電子郵件,我沒有看到任何圖片都和HTML看起來像

<img alt="" hspace="0" src="http://image0" align="baseline" border="0"> 
<br /> 

<img alt="" hspace="0" src="http://image1" align="baseline" border="0"> 
<br /> 

任何線索?

C#就像

var smtp = new SmtpClient(); 

var msg = new MailMessage(new MailAddress("[email protected]", "mysite.com Support"), 
           new MailAddress(email, email)); 
msg.Subject = "No worries, man"; 
msg.IsBodyHtml = true; 
var bodyBuilder = new StringBuilder(); 

for (int i = 0; i < pages.Count; i++) 
    bodyBuilder.AppendLine(string.Format("<img alt=\"\" hspace='0' src='cid:image{0}' align='baseline' border='0'><br />",i)); 
var htmlView = AlternateView.CreateAlternateViewFromString(bodyBuilder.ToString(), null, "text/html"); 

var index = 0; 
foreach (var page in pages) // where page is List<Bitmap> 
{ 
    var memoryStream = new MemoryStream(); 
    page.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); 

    var imagelink = new LinkedResource(memoryStream, "image/png"); 
    imagelink.ContentId = string.Format("image{0}", index); 
    imagelink.TransferEncoding = TransferEncoding.Base64; 

    htmlView.LinkedResources.Add(imagelink); 
    index++; 
} 

msg.AlternateViews.Add(htmlView); 

try 
{ 
    smtp.Send(msg); 
    return Json("true", JsonRequestBehavior.AllowGet); 
} 
catch (Exception ex) 
{ 
    logger.Error(ex.Message, ex); 

    return Json("false", JsonRequestBehavior.AllowGet); 
} 
+0

的HT這些'img'元素上的ml src標記看起來不正確。 – gwin003 2013-05-09 14:23:15

+0

@ gwin003 ...那麼...它應該如何在嵌入圖像的情況下呢?你可以建議嗎? – 2013-05-09 14:24:37

+0

@MichaelTodd對不起...一切都在密碼中,你可以看看這些嗎? – 2013-05-09 14:27:57

回答

0

玉傢伙

我發現這裏的真棒解決方案

How to embed an Image Stream to MailMessage

下面是正確的代碼:

ImageConverter ic = new ImageConverter(); 
Byte [] ba = (Byte[]) ic.ConvertTo(page,typeof(Byte[])); 
var memoryStream = new MemoryStream(ba); 
page.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); 
相關問題