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
Remarks
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