Send Email with Multiple Threads(Mass Mail) in C#

In previous section, I introduced how to use asynchronous mode. In this section, I will introduce how to send mass emails with multiple threads in C#.


Based on asynchronous mode, you can create multiple SmtpClient instances in your application and send email in multiple threads. Here is a simple sample demonstrates how to use asynchronous mode to send email in multiple threads.


Remarks: All of samples in this section are based on first section: Send email in a simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# - Send Mass Emails with Multiple Threads - Example]

The following example codes demonstrate how to send mass email with multiple threads in C#.


To get the full sample projects, please refer to Samples section.

using System;
using System.Collections.Generic;
using System.Text;
using EASendMail; //add EASendMail namespace

namespace mysendemail
    class Program
        static void Main(string[] args)
            string[] arRcpt = new string[]{"",
                "" };

            int nRcpt = arRcpt.Length;
            SmtpMail[] arMail = new SmtpMail[nRcpt];
            SmtpClient[] arSmtp = new SmtpClient[nRcpt];
            SmtpClientAsyncResult[] arResult = new SmtpClientAsyncResult[nRcpt];

            for (int i = 0; i < nRcpt; i++)
                arMail[i] = new SmtpMail("TryIt");
                arSmtp[i] = new SmtpClient();

            for (int i = 0; i < nRcpt; i++)
                SmtpMail oMail = arMail[i];
                // Set sender email address
                oMail.From = "";
                // Set recipient email address
                oMail.To = arRcpt[i];

                // Set email subject
                oMail.Subject = "mass email test from c#";
                // Set email body
                oMail.TextBody = "test from c#, this email is sent to " + arRcpt[i];

                // Your smtp server address
                SmtpServer oServer = new SmtpServer("");

                // User and password for ESMTP authentication
                oServer.User = "";
                oServer.Password = "testpassword";

                // Most mordern SMTP servers require SSL/TLS connection now.
                // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically.
                oServer.ConnectType = SmtpConnectType.ConnectTryTLS;

                // If your SMTP server uses 587 port
                // oServer.Port = 587;

                // If your SMTP server requires SSL/TLS connection on 25/587/465 port
                // oServer.Port = 25; // 25 or 587 or 465
                // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

                SmtpClient oSmtp = arSmtp[i];

                // Submit email to BeginSendMail method and return
                // to process another email
                arResult[i] = oSmtp.BeginSendMail(oServer, oMail, null, null);
                Console.WriteLine(String.Format("Start to send email to {0} ...",
                    arRcpt[i] ));

            // All emails were sent by BeginSendMail Method
            // now get result by EndSendMail method
            int nSent = 0;
            while (nSent < nRcpt)
                for (int i = 0; i < nRcpt; i++)
                    // this email has been sent
                    if (arResult[i] == null)
                    // wait for specified email ...
                    if (!arResult[i].AsyncWaitHandle.WaitOne(10, false))
                        // this email is finished, using EndSendMail to get result
                        Console.WriteLine(String.Format("Send email to {0} successfully",
                    catch (Exception ep)
                           String.Format("Failed to send email to {0} with error {1}: ",
                           arRcpt[i], ep.Message));
                    // Set this email result to null, then it won't be processed again
                    arResult[i] = null;

I strongly suggest that you have a look at Mass sample project, it uses a thread pool to send mass emails and support throttling control.


To get the full sample projects, please refer to Samples section.

Next Section

At next section I will introduce how to send email using EASendMail Service Queue in ASP.NET/C#.



If you have any comments or questions about above example codes, please click here to add your comments.