Send Email with Multiple Threads(Mass Mail) in Managed C++/CLI

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

Introduction

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.

Note

Remarks: All of samples in this section are based on first section: A simple Managed C++/CLI 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.

[Managed C++/CLI Example - Send mass email with multiple threads]

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

Note

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

#include "stdafx.h"

using namespace System;
using namespace EASendMail;

int main(array<System::String ^> ^args)
{
    array<String^> ^arRcpt = {"test1@adminsystem.com",
        "test2@adminsystem.com",
        "test3@adminsystem.com" };

    int nRcpt = arRcpt->Length;
    array<SmtpMail^> ^arMail = gcnew array<SmtpMail^>(nRcpt);
    array<SmtpClient^> ^arSmtp = gcnew array<SmtpClient^>(nRcpt);
    array<SmtpClientAsyncResult^> ^arResult = gcnew array<SmtpClientAsyncResult^>(nRcpt);

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

    for (int i = 0; i < nRcpt; i++)
    {
        SmtpMail ^oMail = arMail[i];

        // Set sender email address
        oMail->From = "sender@emailarchitect.net";
        // Set recipient email address
        oMail->To = arRcpt[i];

        // Set email subject
        oMail->Subject = "mass email test from Managed C++";
        // Set email body
        oMail->TextBody = "test from Managed C++, this email is sent to " + arRcpt[i];

        // Your SMTP server address
        SmtpServer ^oServer = gcnew SmtpServer("smtp.emailarchitect.net");

        // User and password for ESMTP authentication.
        oServer->User = "test@emailarchitect.net";
        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, nullptr, nullptr );
        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] == nullptr)
                continue;
            // wait for specified email ...
            if (!arResult[i]->AsyncWaitHandle->WaitOne(10, false))
            {
                continue;
            }
            try
            {
                // this email is finished, using EndSendMail to get result
                arSmtp[i]->EndSendMail(arResult[i]);
                Console::WriteLine(String::Format("Send email to {0} successfully",
                    arRcpt[i]));
            }
            catch (Exception ^ep)
            {
                Console::WriteLine(
                    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] = nullptr;
            nSent++;
        }
    }

    return 0;
}

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.

Note

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

Next Section

Total sample projects in EASendMail SMTP Component installation package.

Appendix

Comments

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