How To Send Multiple Mails With Multiple Attachments In MVC

  In this article, I'm going to tell you how to send multiple emails with multiple attachments (Sending Bulk Mails) in MVC.
 
Regardless of which platform you are working on like .NET, Python, Java, etc, sending an email is the basic requirement. In a web application, an email may be required to send One Time Passwords (OTPs) for confirmation of user authentication, also for sending notifications, etc. The most valuable service nowadays which comes free of cost is email. To send an email, one should use SMTP (Simple Mail Transfer Protocol ) as a method to transfer mail from one user to another. SMTP is a push-type protocol and is very useful to send the emails whereas POP (post office protocol) or IMAP (internet message access protocol) are used to retrieve those emails at the receiver’s side.
 
To use SMTP, one should have network credentials for authentication purposes.
 
Sending multiple emails is the same as sending a single email. The difference is that to send the multiple emails one should add the emails into a list and pass the list such that email function should loop for the number of mails in it 
 
Source Code
 
First of all, declare the Namespace  "using System.Net.Mail."
 
For basic understanding, I'm giving the source code of three methods which are used to send mail.
 
Code 1
 
This method name is BulkEmail and its return type is a string. This method is just used to call the mail methods which are in void type.
  1. public string BulkEmail()  
  2.         {  
  3.             BuildEmail();  
  4.             return "Success";  
  5.         }  
Code 2
 
In this BuildEmail method, one can append the set of recipient emails to a list and by iterating the for loop, one can send email message to each and every receipient using MailMesage object. In MailMessage object, we need to declare the properties like From, To, Subject, Body, Attachments, etc,. In case, there is a need to send the documents like pdf, doc etc, one can attach those documents as email attachments and assign to MailMessage object.
  1. public static void BuildEmail()  
  2.         {  
  3.             ArrayList list_emails = new ArrayList();  // In this arraylist you can set the group of mails that you want to communicate  
  4.             list_emails.Add("[email protected]");  
  5.             list_emails.Add("[email protected]");  
  6.             foreach (string email_to in list_emails)  
  7.             {  
  8.                 Random rnd = new Random();  
  9.                 int otp = rnd.Next(1000, 9999);      // If you need you can generate otp and can send as message            
  10.                 string[] filepathhs = Directory.GetFiles(@"D:\EmailProcess""*pdf");                 
  11.                 string msg = "your otp is " + otp;                 
  12.                 MailMessage message = new MailMessage();  
  13.                 message.From = new MailAddress("[email protected]");   // Here you need to declare the from mail address  
  14.                 message.To.Add(new MailAddress(email_to));  // here in general you need to declare the recepient mail, but here it will assigning from array list declared above  
  15.                 message.Subject = "This is for test";    // Here you can declare the subject content  
  16.                 message.Body = "Hi Santhosh Your Email process is Success" + msg + "";      // Here you can add your specific message   
  17.   
  18.                 foreach (var filepath in filepathhs)  
  19.                 {  
  20.                     var attachment = new Attachment(filepath);   // here you can attach a file as a mail attachment  
  21.                     message.Attachments.Add(attachment);  
  22.                 }  
  23.                 SendEmail(message);    // This invokes the mail connection properties method  
  24.             }  
  25.         }  
Code 3
 
This is the method where one can configure the credentials over SMTP Client by giving Host, Port details and the network credentials.
  1. public static void SendEmail(MailMessage message)  
  2.         {  
  3.             System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();  
  4.             client.Host = "smtp.gmail.com";     // These are the host connection properties  
  5.             client.Port = 587;  
  6.             client.EnableSsl = true;  
  7.             client.DeliveryMethod = SmtpDeliveryMethod.Network;  
  8.             client.Credentials = new System.Net.NetworkCredential("[email protected]""yourmailpassword");    // here you need to declare the credentials of the sender  
  9.             try  
  10.             {  
  11.                 client.Send(message);      // And finally this is the line which executes our process and sends mail             
  12.             }  
  13.             catch (Exception ex)  
  14.             {  
  15.                 throw ex;  
  16.             }    
  17.         }  
So that's it. Now you can send multiple attachments to multiple mails using C#. 
 
Thanks for reading the article.
 
All the best for your future endeavours!


Similar Articles