SmtpClient.BatchSendMail Method


Sends bulk email messages with multiple threads.

[Visual Basic]
Public Sub BatchSendMail( _
    maxThreads As Integer , _
    servers() As SmtpServer, _
    mails() As SmtpMail _
)
[C#]
public void BatchSendMail(
    int maxThreads,
    SmtpServer[] servers,
    SmtpMail[] mails 
);
[C++]
public: Void BatchSendMail(
    int maxThreads,
    array<SmtpServer>^ servers,
    array<SmtpMail>^ mails
);
[JScript]
public function BatchSendMail( 
    maxThreads: int,
    servers : SmtpServer [], 
    mails : SmtpMail []   
);

Parameters

maxThreads
The maximum value of worker threads count to send email.
servers
A SmtpServer array instances used to send email.
mails
A SmtpMail array instance to send.

Remarks

Basically, we don't recommend to use this method. To send email with multiple threads in desktop application, we suggest that you use the BeginSendMail method, for more detail, please refer to mass.vb and mass.csharp samples in EASendMail installation package; To send bulk emails in web application, we suggest that you use the EASendMail Service, please refer to Work with EASendMail Service (Email Queuing).
Only in the following case, we recommend that you use this method.
You need to send bulk emails in web application and your website is hosted by ISP, you don't have the permission to install the EASendMail Service.
If you use BatchSendMail method to send bulk emails in ASP.NET page, the advantage is: no matter how many emails you need to send, this method returns immediately, EASendMail will send the emails in background; The disadvantage is: you can't get the result immediately, you have to trace the email by log file or get the result by catching the OnBatchSendMail event.

Example

[Visual Basic, C#] To get the full samples of EASendMail, please refer to Samples section.

[VB - Send Bulk Email with Multiple Threadings]

Imports EASendMail

Public Shared Sub OnBatchSendMail( _
    sender As Object, _
    server As SmtpServer, _
    mail As SmtpMail, _
    ep As Exception, _
    ByRef cancel As Boolean )
    
    ' you can insert the result to database in this subroutine.
    If Not (ep Is Nothing) Then
        ' something wrong, please refer to ep.Message
        ' cancel = True ' set cancel to true can cancel the remained emails.
    Else
         ' delivered
    End If
End Sub

Public SendMail( _
    fromAddr As String, _
    rcpts As String, _
    server As String )
    
    Dim oAddrs As New AddressCollection(rcpts)
    Dim count As Integer = oAddrs.Count
    
    ' the maximum thread count to send email. you can increase or decrease this value.
    Dim maxThreads As Integer = 5 
    Dim mails(count-1) As SmtpMail
    Dim servers(count-1) As SmtpServer
            
    For i As Integer = 0 To count - 1

        Dim oMail As SmtpMail = New SmtpMail("TryIt")
        oMail.From = New MailAddress(fromAddr)
        oMail.To.Add(oAddrs(i))

        oMail.Subject = "test subject"
        oMail.TextBody = String.Format("test body to {0}", oAddrs(i).ToString())

        mails(i) = oMail
    Next
    
    Dim oServer As SmtpServer = New SmtpServer(server)
    servers(0) = oServer
    
    Dim oSmtp As SmtpClient = New SmtpClient()
    
    Try  
        ' oSmtp.LogFileName = "c:\smtp.log"
        ' if the log wasn't able to be generated, 
        ' please create a smtp.log file on C: and assign everyone read-write permission to this
        ' file, then try it again.
        
        ' if you want to catch the result in OnBatchSendMail, please add the following code:
        ' AddHandler oSmtp.OnBatchSendMail, AddressOf OnBatchSendMail
        
        oSmtp.BatchSendMail(maxThreads, servers, mails)
        Response.Write("EASendMail will send emails in background!")
    Catch exp As Exception
        Response.Write(String.Format("Exception: Common: {0}", exp.Message))
    End Try
End Sub


[C# - Send Bulk Email with Multiple Threadings] using System; using EASendMail; public static void OnBatchSendMail( object sender, SmtpServer server, SmtpMail mail, Exception ep, ref bool cancel) { // you can insert the result to database in this subroutine. if(ep != null) { // something wrong, please refer to ep.Message // cancel = true; // set cancel to true can cancel the remained emails. } else { // delivered } } public void SendMail( string fromAddr, string rcpts, string server ) { AddressCollection oAddrs = new AddressCollection(rcpts); int count = oAddrs.Count; // the maximum thread count to send email. you can increase or decrease this value. int maxThreads = 5; SmtpMail [] mails = new SmtpMail[count]; SmtpServer[] servers = new SmtpServer[1]; for(int i = 0; i < count; i++) { SmtpMail oMail = new SmtpMail("TryIt"); oMail.From = fromAddr; oMail.To.Add(oAddrs[i]); oMail.Subject = "test subject"; oMail.TextBody = String.Format("test body to {0}", oAddrs[i].ToString()); mails[i] = oMail; } SmtpServer oServer = new SmtpServer(server); servers[0] = oServer; SmtpClient oSmtp = new SmtpClient(); try { // oSmtp.LogFileName = "c:\\smtp.log"; // if the log wasn't able to be generated, // please create a smtp.log file on C: and assign everyone read-write permission to this // file, then try it again. // if you want to catch the result in OnBatchSendMail event, please add the following code // oSmtp.OnBatchSendMail += new EASendMail.SmtpClient.OnBatchSendMailEventHandler(OnBatchSendMail); oSmtp.BatchSendMail(maxThreads, servers, mails); Response.Write("EASendMail will send message in background!"); } catch(Exception exp) { Response.Write(String.Format("Exception: Common: {0}", exp.Message)); } }

See Also

Work with EASendMail Service (Email Queuing)
SmtpClient.SendMailToQueue Method
SmtpClient.SendMailToQueueEx Method
SmtpClient.OnBatchSendMail Event