【问题标题】:sending image with other details in mail body在邮件正文中发送带有其他详细信息的图像
【发布时间】:2017-04-27 02:57:57
【问题描述】:

我正在尝试将产品详细信息发送到电子邮件正文,但图像未显示

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); string To = user2, UserID, Password, SMTPPort, Host;

            mail.To.Add(To);
            mail.From = new MailAddress("emailid");
            string subject = "Order Confirmation - Your Order with kasmiriproducts.com [ORDER1234567] has been successfully placed!";

            StringBuilder sb = new StringBuilder();
            sb.Append("<b>Hi " + firstName + " " + lastName + "</b><br />");
            sb.Append("Thank you for your order!<br />");
            sb.Append("Please find below, the summary of your order ORDER1234567<br /><hr>");
            sb.Append("<section class='cart-area'><div class='container'><div id='cart'><div id='popupcart' class='table-responsive'><table class='' cellpadding='10' cellspacing='10'><tr class='cartHeaders'><th class='itemImage'>Image</th><th class='itemName'>Name</th><th class='itemProductSize'>ProductSize</th><th class='itemPrice'>Price</th><th class='itemQuantity'>Quantity</th><th class='itemSub Total(&amp;#8377;)'>Sub Total(₹)</th></tr>");     
            foreach (var x in order)
            {
                sb.Append("<tr class='itemContainer'>");
                sb.Append("<td class='itemImage'><img src='../images/" + x.product.image + "'></td>");
                sb.Append("<td class='itemName'>" + x.product.productname + "</td>");
                sb.Append("<td class='itemProductSize'>" + x.psize.size + "</td>");
                sb.Append("<td class='itemPrice'>" + x.productsize.price + "</td>");
                sb.Append("<td class='itemQuantity'>" + x.quantity + "</td>");
                sb.Append("<td class='itemSub Total(&amp;#8377;)'>" + x.quantity + "</td>");
                sb.Append("</tr>");                         
            }
            sb.Append("</table></div></div></div></section>");
            ViewBag.testing = sb.ToString();
            mail.Body = sb.ToString();
            mail.IsBodyHtml = true;
            mail.Subject = subject;
            SmtpClient sc = new SmtpClient("smtp.gmail.com");
            sc.EnableSsl = true;
            sc.UseDefaultCredentials = false;
            sc.Credentials = new System.Net.NetworkCredential("emailid",
                "password");
            sc.Send(mail);

【问题讨论】:

  • 你应该使用绝对 URL 而不是相对 URL,我的意思是 src='../images/" 部分不起作用。
  • 还是不行
  • 确保图片资源可以从互联网访问。

标签: .net


【解决方案1】:

您不应在电子邮件正文中添加图像文件的物理/相对路径,因为电子邮件客户端很可能无法在物理位置访问图像。正确的方法是通过将图像作为电子邮件附件附加,然后在邮件正文 HTML 中图像的源属性中使用附加图像的内容 ID,将图像嵌入到您的电子邮件中。
代码摘录如下:-

Attachment att = mail.AddAttachment( "d:\\img\\image.jpg" ); //Path to the image file
string contentId = "Image1";
att.ContentID = contentId; //content id can be any string value
mail.HtmlBody = "<html><body><img src=\"cid:" + contentId + "\"></body></html>"; //Use content id as image source

参考链接:-
https://www.emailarchitect.net/easendmail/ex/c/15.aspx
Send inline image in email

【讨论】:

    【解决方案2】:

    我能够使用

    在电子邮件正文中发送图像
       **emailbody.Append("<img src="imagePathHere">");**
    

    通过这行代码,我的图像被嵌入到我的电子邮件正文中

    【讨论】:

      猜你喜欢
      • 2014-07-12
      • 2014-01-26
      • 1970-01-01
      • 2021-01-19
      • 2017-01-25
      • 2017-10-28
      • 1970-01-01
      • 2020-09-22
      • 2017-11-25
      相关资源
      最近更新 更多