2014-10-22 11 views
0

Я пытаюсь добавить изображение в HTML-почту. Он отображает, когда я сохраняю что тела в HTML файла, но когда я посылаю, что HTML тела по электронной почте через GMail, изображение не отображается. Может ли кто-нибудь сказать мне причину?Почему изображение не отображается в html-письме в gmail?

Я установил источник изображения таким способом:

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

Добро пожаловать в переполнение стека. Было бы лучше, если мы увидим вашу работу. Вы пытались установить свойство ['IsBodyHtml'] (http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.isbodyhtml.aspx)? Пожалуйста, прочитайте [FAQ], [ask] и [help] в качестве старта .. –

+0

Пожалуйста, введите код –

+0

@VinodVT Я опубликовал код – Fahad

ответ

0

Более вероятно, ваш образ атрибут SRC не является абсолютным, publicly- доступный URI. Любая файловая система или локальный URI не будет показывать изображение в письме.

В частности, они не будут работать:

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

Убедитесь, что ваши URLs изображения являются

  • абсолютными (т.е. начать с протоколом, как http://)
  • включает в себя полный путь к изображению
  • домен является общественным достоянием
+0

вы можете использовать LinkedResource. Он работает на 100% – Dan

-1

Try кодовую часть из 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(); 
Смежные вопросы