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

Introduction

In my previous article I explained Send Email Using ASP.Net With C#. In this article I will explain how to send multiple emails with attachments. If you don't bother about the code because I just change some codes in my previous article. ! Let's start.

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"//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.     if (FileUpload2.HasFile)//Attaching document    
  15.     {    
  16.         string FileName = Path.GetFileName(FileUpload2.PostedFile.FileName);    
  17.         message.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileName));    
  18.   
  19.     }    
  20.   
  21.     string mailbody = Txt_Bodycontent.Text;    
  22.     message.Subject = Txt_Subject.Text;    
  23.     message.Body = mailbody;    
  24.     message.BodyEncoding = Encoding.UTF8;    
  25.     message.IsBodyHtml = true;    
  26.     SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp        
  27.     System.Net.NetworkCredential basicCredential1 = new    
  28.     System.Net.NetworkCredential("fromaddress""fromaddresspassword");    
  29.     client.EnableSsl = true;    
  30.     client.UseDefaultCredentials = false;    
  31.     client.Credentials = basicCredential1;    
  32.     try    
  33.     {    
  34.         client.Send(message);    
  35.     }    
  36.   
  37.     catch (Exception ex)    
  38.     {    
  39.         throw ex;    
  40.     }    
  41. }  
We must add the following namespace: 
  1. using System.Net;  
  2. using System.Net.Mail;  
  3. using System.Text;  
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"//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. }  

Attachment

If you want to send an attachment with email you can add the following code. Otherwise you just remove this part in the code.
  1. if (FileUpload2.HasFile)//Attaching document  
  2. {  
  3.     string FileName = Path.GetFileName(FileUpload2.PostedFile.FileName);  
  4.     message.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileName));  
  5.  
  6. }  
File Upload

If you want to select multiple files in a single file uploader, then you just add AllowMultiple="true". This is one of the option in ASP file upload.
  1. <div>  
  2.     <asp:FileUpload runat="server" ID="FileUpload2" AllowMultiple="true" />  
  3. </div>  
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 attachment using ASP.NET and C#. I hope this article is useful for all .NET beginners.

Up Next
    Ebook Download
    View all
    Learn
    View all