发送图像与邮件正文中的其他详细信息

时间:2016-12-12 09:19:40

标签: .net

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

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我能够使用

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

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