0

I am trying to send product details i email body but image is not getting display

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);
JSS
  • 21
  • 1
  • 12

2 Answers2

1

You should not add physical/relative path to image file in your email body as the email client will most likely be not able to access the image at the physical location. Right approach would be to embed image with your email by attaching the image as an attachment with the email and then using content id of the attached image in the images' source attribute in mail body HTML.
Code excerpt below:-

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

Reference links:-
https://www.emailarchitect.net/easendmail/ex/c/15.aspx
Send inline image in email

Community
  • 1
  • 1
Vineet Desai
  • 872
  • 5
  • 16
0

I was able to send image within email body using

   **emailbody.Append("<img src="imagePathHere">");**

Through this line of code my image was embedded in the body of my email

Nida Akram
  • 332
  • 2
  • 9
  • 24