2014-10-22 7 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

스택 오버플로에 오신 것을 환영합니다. 우리가 당신의 일을 또한 보는 것이 좋을 것입니다. ['IsBodyHtml' 속성을 설정하려고 시도 했습니까 (http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.isbodyhtml.aspx)? –

+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 

  • 이미지의 전체 경로를 포함 (http:// 같은 프로토콜로 시작 예) 이미지 URL이

    • 절대 있는지 확인
    • 도메인은 공개 도메인입니다.
  • +0

    당신은 LinkedResource를 사용할 수 있습니다. 100 % 확실하게 작동합니다. – Dan

    -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(); 
    
    관련 문제