Send Email To Multiple Email Addresses With Inline Image Using ASP.NET With C#

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.   
  4.     string to = Txt_toaddress.Text; //To address        
  5.     string from = "fromaddress email"//From address     
  6.     string[] Multiple = to.Split(',');    
  7.     MailMessage message = new MailMessage();    
  8.     message.From = new MailAddress(from);    
  9.   
  10.     foreach (string multiple_email in Multiple)    
  11.     {    
  12.         message.To.Add(new MailAddress(multiple_email));    
  13.     }    
  14.   
  15.   
  16.     string mailbody = Txt_Bodycontent.Text + "<br/><html><body><h1>Happy Coding</h1><br>            <img src=\"cid:Email\"  width='600' height='300'></body></html>";    
  17.     AlternateView AlternateView_Html = AlternateView.CreateAlternateViewFromString(mailbody, null, MediaTypeNames.Text.Html);    
  18.     // Create a LinkedResource object and set Image location path and Type    
  19.     LinkedResource Picture1 = new LinkedResource(Server.MapPath("Selfie.jpeg"), MediaTypeNames.Image.Jpeg);    
  20.     Picture1.ContentId = "Email";    
  21.     AlternateView_Html.LinkedResources.Add(Picture1);    
  22.     message.AlternateViews.Add(AlternateView_Html);    
  23.   
  24.     message.Subject = Txt_Subject.Text;    
  25.     message.Body = mailbody;    
  26.     message.BodyEncoding = Encoding.UTF8;    
  27.     message.IsBodyHtml = true;    
  28.     SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp        
  29.     System.Net.NetworkCredential basicCredential1 = new    
  30.     System.Net.NetworkCredential("fromaddress email""fromaddress password");    
  31.     client.EnableSsl = true;    
  32.     client.UseDefaultCredentials = false;    
  33.     client.Credentials = basicCredential1;    
  34.     try    
  35.     {    
  36.         client.Send(message);    
  37.     }    
  38.   
  39.     catch (Exception ex)    
  40.     {    
  41.         throw ex;    
  42.     }    
  43. }    
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.   
  7. foreach (string multiple_email in Multiple)  
  8. {  
  9.     message.To.Add(new MailAddress(multiple_email));  
  10. }  
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.

Up Next
    Ebook Download
    View all
    Learn
    View all