【问题标题】:Why image is not displaying in html mail in gmail?为什么图像没有显示在 gmail 的 html 邮件中?
【发布时间】:2014-12-17 14:10:19
【问题描述】:

我正在尝试在 HTML 邮件中添加图像。当我将该 body 保存为 html 文件,但当我通过 GMail 将该 html 正文 作为电子邮件发送时,它会显示strong>,图像不显示。谁能告诉我原因?

我是这样设置图片来源的:

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) { }
}

【问题讨论】:

  • 欢迎来到 Stack Overflow。如果我们也看到你的作品会更好。您是否也尝试设置您的IsBodyHtml property?请阅读FAQHow to Askhelp center 作为开始..
  • 请发布您的代码
  • @VinodVT 我已经发布了代码
  • @SonerGönül 是的,我已将其设置为真的。我发布了我用来发送电子邮件的方法的代码。
  • 您是否在收到邮件时尝试检查邮件?你可以查看它的源代码。

标签: c# html email


【解决方案1】:

您的图像 SRC 属性很可能不是绝对的、可公开访问的 URI。任何文件系统或本地 URI 都不会在电子邮件中显示图像。

具体来说,这些不起作用:

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

确保您的图片网址是

  • 绝对(即以http://之类的协议开头)
  • 包含图片的完整路径
  • 该域是公共域

【讨论】:

    【解决方案2】:

    试试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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 2018-09-05
      相关资源
      最近更新 更多