SmtpClient.OnBatchSendMail Event


Occurs when an email was sent by BatchSendMail method.

[Visual Basic]
Public Event OnBatchSendMail As OnBatchSendMailEventHandler
Public Delegate Sub OnBatchSendMailEventHandler( _
    ByVal sender As Object, _
    ByVal server As SmtpServer, _
    ByVal mail As SmtpMail, _
    ByVal ep As Exception, _
    ByRef cancel As Boolean _
)
[C#]
public event OnBatchSendMailEventHandler OnBatchSendMail;
public delegate void OnBatchSendMailEventHandler( 
     object sender, 
     SmtpServer server,
     SmtpMail mail,
     Exception ep,
     ref bool cancel 
);
[C++]
public: __event OnBatchSendMailEventHandler^ OnBatchSendMail;
public __gc __delegate void OnBatchSendMailEventHandler( 
     Object* sender, 
     SmtpServer ^server,
     SmtpMail ^mail,
     Exception ^ep,     
     Boolean % cancel 
);
[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

sender
The source (SmtpClient instance) of the event.
server
The SmtpServer instance to send the email.
mail
The SmtpMail instance to send.
ep
The Exception instance indicating whether there is an error.
cancel
Gets or sets a value indicating whether the task (BatchSendMail) should be canceled.

Remarks

If cancel parameter is set to true in the event, the client terminates the sending immediately.
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

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