Introduction
In this article I will explain how to send multiple emails with Inline Image. Before reading this article you must read my previous email related article because I have explained some basics part in my previous article and changed few of the code part in new one. The following are my previous articles.
Code
  1. protected void btn_sendemail_Click(object sender, EventArgs e)
  2. {
  3. string to = Txt_toaddress.Text; //To address
  4. string from = "fromaddress email"; //From address
  5. string[] Multiple = to.Split(',');
  6. MailMessage message = new MailMessage();
  7. message.From = new MailAddress(from);
  8. foreach (string multiple_email in Multiple)
  9. {
  10. message.To.Add(new MailAddress(multiple_email));
  11. }
  12. string mailbody = Txt_Bodycontent.Text + "<br/><html><body><h1>Happy Coding</h1><br> <img src=\"cid:Email\" width='600' height='300'></body></html>";
  13. AlternateView AlternateView_Html = AlternateView.CreateAlternateViewFromString(mailbody, null, MediaTypeNames.Text.Html);
  14. // Create a LinkedResource object and set Image location path and Type
  15. LinkedResource Picture1 = new LinkedResource(Server.MapPath("Selfie.jpeg"), MediaTypeNames.Image.Jpeg);
  16. Picture1.ContentId = "Email";
  17. AlternateView_Html.LinkedResources.Add(Picture1);
  18. message.AlternateViews.Add(AlternateView_Html);
  19. message.Subject = Txt_Subject.Text;
  20. message.Body = mailbody;
  21. message.BodyEncoding = Encoding.UTF8;
  22. message.IsBodyHtml = true;
  23. SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp
  24. System.Net.NetworkCredential basicCredential1 = new
  25. System.Net.NetworkCredential("fromaddress email", "fromaddress password");
  26. client.EnableSsl = true;
  27. client.UseDefaultCredentials = false;
  28. client.Credentials = basicCredential1;
  29. try
  30. {
  31. client.Send(message);
  32. }
  33. catch (Exception ex)
  34. {
  35. throw ex;
  36. }
  37. }
We must add the following namespace:
  1. using System.Net;
  2. using System.Net.Mail;
  3. using System.Text;
  4. using System.IO;
  5. using System.Net.Mime;
Multiple Email
The following code will help to split the Comma '"," separated email in the given textbox ( Txt_toaddress.Text ).
  1. string to = Txt_toaddress.Text; //To address
  2. string from = "fromaddress email"; //From address
  3. string[] Multiple = to.Split(',');
  4. MailMessage message = new MailMessage();
  5. message.From = new MailAddress(from);
  6. foreach (string multiple_email in Multiple)
  7. {
  8. message.To.Add(new MailAddress(multiple_email));
  9. }
Inline Image Code
The following code will help to fetch the image and set into the inline of the email body.
  1. string mailbody = Txt_Bodycontent.Text + "<br/><html><body><h1>Happy Coding</h1><br><img src=\"cid:Email\" width='600' height='300'></body></html>";
  2. AlternateView AlternateView_Html = AlternateView.CreateAlternateViewFromString(mailbody, null, MediaTypeNames.Text.Html);
  3. // Create a LinkedResource object and set Image location path and Type
  4. LinkedResource Picture1 = new LinkedResource(Server.MapPath("Selfie.jpeg"), MediaTypeNames.Image.Jpeg);
  5. Picture1.ContentId = "Email";
  6. AlternateView_Html.LinkedResources.Add(Picture1);
  7. message.AlternateViews.Add(AlternateView_Html);
  8. message.Body = mailbody;
Important Notes
  • Firstly, create one string that contain the html body with Inline image. In the above code "mailbody" is the string. It contains the html body.

  • Create an AlternateView object for those supporting the HTML content.

  • System.Net.Mime namespace contain the Image type and html format ( MediaTypeNames.Image.Jpeg & MediaTypeNames.Text.Html ).

  • Create a LinkedResource object for the Inline image to send.

  • Add a LinkedResource object to the AlternateView object.

  • Check the correct Image location otherwise it will throw an error.

  • Give same Image source id and LinkedResource ContentId.Like "<img src=\"cid:Email\" width='600' height='300'>" & Picture1.ContentId = "Email";
Design
Output
Common Error for sending an Email
Check the following reference to solve your 5.5.1 Authentication.
Reference: You can also see this in my blog:
Summary
We learned how to send multiple emails with Inline Image using ASP.NET and C#. I hope this article is useful for all .NET beginners.