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. string to = Txt_toaddress.Text; //To address
  4. string from = "fromaddress"; //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. if (FileUpload2.HasFile)//Attaching document
  13. {
  14. string FileName = Path.GetFileName(FileUpload2.PostedFile.FileName);
  15. message.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileName));
  16. }
  17. string mailbody = Txt_Bodycontent.Text;
  18. message.Subject = Txt_Subject.Text;
  19. message.Body = mailbody;
  20. message.BodyEncoding = Encoding.UTF8;
  21. message.IsBodyHtml = true;
  22. SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp
  23. System.Net.NetworkCredential basicCredential1 = new
  24. System.Net.NetworkCredential("fromaddress", "fromaddresspassword");
  25. client.EnableSsl = true;
  26. client.UseDefaultCredentials = false;
  27. client.Credentials = basicCredential1;
  28. try
  29. {
  30. client.Send(message);
  31. }
  32. catch (Exception ex)
  33. {
  34. throw ex;
  35. }
  36. }
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. foreach (string multiple_email in Multiple)
  7. {
  8. message.To.Add(new MailAddress(multiple_email));
  9. }

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. }
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.