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.
- public string BulkEmail()
- {
- BuildEmail();
- return "Success";
- }
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.
- public static void BuildEmail()
- {
- ArrayList list_emails = new ArrayList(); // In this arraylist you can set the group of mails that you want to communicate
- list_emails.Add("[email protected]");
- list_emails.Add("[email protected]");
- foreach (string email_to in list_emails)
- {
- Random rnd = new Random();
- int otp = rnd.Next(1000, 9999); // If you need you can generate otp and can send as message
- string[] filepathhs = Directory.GetFiles(@"D:\EmailProcess", "*pdf");
- string msg = "your otp is " + otp;
- MailMessage message = new MailMessage();
- message.From = new MailAddress("[email protected]"); // Here you need to declare the from mail address
- 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
- message.Subject = "This is for test"; // Here you can declare the subject content
- message.Body = "Hi Santhosh Your Email process is Success" + msg + ""; // Here you can add your specific message
- foreach (var filepath in filepathhs)
- {
- var attachment = new Attachment(filepath); // here you can attach a file as a mail attachment
- message.Attachments.Add(attachment);
- }
- SendEmail(message); // This invokes the mail connection properties method
- }
- }
This is the method where one can configure the credentials over SMTP Client by giving Host, Port details and the network credentials.
- public static void SendEmail(MailMessage message)
- {
- System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
- client.Host = "smtp.gmail.com"; // These are the host connection properties
- client.Port = 587;
- client.EnableSsl = true;
- client.DeliveryMethod = SmtpDeliveryMethod.Network;
- client.Credentials = new System.Net.NetworkCredential("[email protected]", "yourmailpassword"); // here you need to declare the credentials of the sender
- try
- {
- client.Send(message); // And finally this is the line which executes our process and sends mail
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
Thanks for reading the article.
All the best for your future endeavours!

muhammad raazaPosted Dec 28, 2022, 11:57 AM
Brillient work GOD bless you i have a question can you help me please
RaviTeja KandukuriPosted Dec 18, 2019, 6:30 AM
Done with dis, much thanks to you!
Chandhu YerramsettiPosted Dec 18, 2019, 6:27 AM
Nice, Can I use my outlook mail account for sending?
Yeela SinghPosted Dec 18, 2019, 6:25 AM
Nice sir, Thanks for Sharing
Sugun RoyPosted Dec 18, 2019, 6:23 AM
Nice article! worked like a charm
Sourav Kumar DasPosted Dec 17, 2019, 10:49 PM
Nice and useful article.
Ravishankar NPosted Dec 17, 2019, 10:46 AM
Nice article