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();
- 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);
- string[] filepathhs = Directory.GetFiles(@"D:\EmailProcess", "*pdf");
- string msg = "your otp is " + otp;
- MailMessage message = new MailMessage();
- message.From = new MailAddress("[email protected]");
- message.To.Add(new MailAddress(email_to));
- message.Subject = "This is for test";
- message.Body = "Hi Santhosh Your Email process is Success" + msg + "";
-
- foreach (var filepath in filepathhs)
- {
- var attachment = new Attachment(filepath);
- message.Attachments.Add(attachment);
- }
- SendEmail(message);
- }
- }
Code 3
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";
- client.Port = 587;
- client.EnableSsl = true;
- client.DeliveryMethod = SmtpDeliveryMethod.Network;
- client.Credentials = new System.Net.NetworkCredential("[email protected]", "yourmailpassword");
- try
- {
- client.Send(message);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
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!