2014-10-22 74 views
0

我試圖在HTML郵件中添加圖像。當我保存它顯示的是HTML文件,但是當我發送HTML體作爲發電子郵件,通過的GMail,圖像不顯示。誰能告訴我原因?爲什麼圖像不顯示在Gmail郵件中的HTML郵件?

我設置圖像的這樣來源:

var image = body.GetElementsByTagName("img"); 
string imageAttachmentPath = Path.Combine(Globals.NotificationTemplatesPath, "Header.png"); 
foreach (XmlElement img in image) 
{ 
    img.SetAttribute("src", imageAttachmentPath); 
    break; 
} 

//thats the method in which i am sending email. 

public static void SendMessageViaEmailService(string from, 
               string sendTo, 
               string carbonCopy, 
               string blindCarbonCopy, 
               string subject, 
               string body, 
               bool isBodyHtml, 
               string imageAttachmentPath, 
               Hashtable images, 
               List<string> attachment, 
               string title = null, 
               string embeddedImages = null) 
{ 
    Attachment image = new Attachment(imageAttachmentPath); 
    MailMessage msg = new MailMessage(); 
    msg.IsBodyHtml = true;     // email body will allow html elements 
    msg.From = new MailAddress(from, "Admin"); // setting the Sender Email ID 
    msg.To.Add(sendTo);      // adding the Recipient Email ID 

    if (!string.IsNullOrEmpty(carbonCopy))  // add CC email ids if supplied. 
     msg.CC.Add(carbonCopy); 

    msg.Subject = subject;      //setting email subject and body 
    msg.Body = body; 
    msg.Attachments.Add(image); 

    //create a Smtp Mail which will automatically get the smtp server details 
    //from web.config mailSettings section 
    SmtpClient SmtpMail = new SmtpClient(); 
    SmtpMail.Host = "smtp.gmail.com"; 
    SmtpMail.Port = 587; 
    SmtpMail.EnableSsl = true; 
    SmtpMail.UseDefaultCredentials = false; 
    SmtpMail.Credentials = new System.Net.NetworkCredential(from, "password"); 

    // sending the message. 
    try 
    { 
     SmtpMail.Send(msg); 
    } 
    catch (Exception ex) { } 
} 
+1

歡迎來到Stack Overflow。如果我們也看到你的工作會更好。你有沒有嘗試設置你的['IsBodyHtml'屬性](http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.isbodyhtml.aspx)?請閱讀[常見問題],[問]和[幫助]作爲開始.. – 2014-10-22 07:16:40

+0

請發表您的代碼 – 2014-10-22 07:20:53

+0

@VinodVT我已發佈代碼 – Fahad 2014-10-22 07:29:57

回答

0

更可能的是你的形象SRC屬性不是絕對的,公帑可訪問的URI。任何文件系統或本地URI都不會在電子郵件中顯示圖像。

具體而言,這些都不行:

c://test.png 
test.png 
/folder/test.png 
http://localhost/test.png 
http://internaldomain/test.png 

確保您的圖片網址

  • 絕對的(即使用類似http://協議)
  • 包括完整路徑圖像
  • 該域名是公共領域
+0

你可以使用LinkedResource。它確實有效 – Dan 2015-09-29 19:04:19

-1

this thread嘗試的代碼部分:

// creating the attachment 
System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"c:\\test.png"); 
inline.ContentDisposition.Inline = true; 
// sending the message 
MailMessage email = new MailMessage(); 
// set the information of the message (subject, body ecc...) 

// send the message 
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost"); 
smtp.Send(email); 
email.Dispose(); 
相關問題