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 VB.NET.
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: Send email in a simple VB.NET 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.
The following example codes demonstrate how to send mass email with multiple threads in VB.NET.
Note
To get the full sample projects, please refer to Samples section.
Imports EASendMail 'Add EASendMail namespace
Module Module1
Sub Main()
Dim arRcpt() As String = {"test1@adminsystem.com", _
"test2@adminsystem.com", _
"test3@adminsystem.com"}
Dim nRcpt As Integer = arRcpt.Length
Dim arMail(nRcpt - 1) As SmtpMail
Dim arSmtp(nRcpt - 1) As SmtpClient
Dim arResult(nRcpt - 1) As SmtpClientAsyncResult
For i As Integer = 0 To nRcpt - 1
arMail(i) = New SmtpMail("TryIt")
arSmtp(i) = New SmtpClient()
Next
For i As Integer = 0 To nRcpt - 1
Dim oMail As SmtpMail = 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 vb.net"
' Set email body
oMail.TextBody = "test from VB, this email is sent to " + arRcpt(i)
' Your SMTP server address
Dim oServer As New 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
Dim oSmtp As SmtpClient = arSmtp(i)
' Submit email to BeginSendMail method and return
' to process another email
arResult(i) = oSmtp.BeginSendMail(oServer, oMail, Nothing, Nothing)
Console.WriteLine(String.Format("Start to send email to {0} ...", _
arRcpt(i)))
Next
' all emails were sent by BeginSendMail Method
' now get result by EndSendMail method
Dim nSent As Integer = 0
Do While (nSent < nRcpt)
For i As Integer = 0 To nRcpt - 1
' this email is finished
If (arResult(i) Is Nothing) Then
Continue For
End If
' wait for specified email ...
If (Not arResult(i).AsyncWaitHandle.WaitOne(10, False)) Then
Continue For
End If
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 ep As Exception
Console.WriteLine( _
String.Format("Failed to send email to {0} with error {1}: ", _
arRcpt(i), ep.Message))
End Try
' Set this email result to null, then it won't be processed again
arResult(i) = Nothing
nSent += 1
Next
Loop
End Sub
End Module
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
At next section I will introduce how to send email using EASendMail Service Queue in ASP.NET/VB.
Appendix
Comments
If you have any comments or questions about above example codes, please click here to add your comments.