ANSMTP Developers Center > Threading-Pool in .NET
Introduction
To enhance the performance in sending mass mail, MassSender Object, which is designed based on multi-threading algorithm, is added to ANSMTP. This tutorial shows how to use MassSender in your application.
Send mass emails using MassSender.
Installation and Deployment
You should download the ansmtp installer and install it on your machine at first. If you want to distribute or deploy ansmtp without ansmtp installer, please click here to learn more.
How does it work?
MassSender has an inner threading pool based on MaxThreads count. Firstly, Send or SendByPickup submits email to MassSender mail queue. Secondly threading pool retrieves email from mail queue and send it out. Finally OnSent event informs that the email was sent successfully or unsuccessfully.
No. of worker threads in the threading pool of MassSender is automatically adjusted based on the actual usage. The maximum no. of worker threads is up to the value of MaxThread property specified.
Simple Sample
The following code demonstrates how to use MassSender object to send mass emails.
using System;
using ANSMTPLib;
namespace MassSenderConsole
{
class MassSender
{
private static MassSenderClass m_oMassSender = null;
private static OBJClass m_oSmtp = null;
[STAThread]
static void Main(string[] args)
{
string []recipientAddr = new string[3];
if( m_oMassSender == null || m_oSmtp == null )
{
m_oMassSender = new MassSenderClass();
m_oSmtp = new OBJClass();
m_oMassSender.MaxThreads = 10; //set the maximum worker threads
bind OnSent event to OnSent method
_IMassSenderEvents_OnSentEventHandler OnSentEventHandler =
new _IMassSenderEvents_OnSentEventHandler(OnSent);
m_oMassSender.OnSent += OnSentEventHandler;
}
m_oSmtp.FromAddr = "test@adminsystem.net";
m_oSmtp.ServerAddr = "mail.adminsystem.net";
//if you don't have a SMTP server, use the following code:
//MassSender sends email via dns lookup
//m_oSmtp.ServerAddr = ""
recipientAddr[0] = "test@adminsystem.net";
recipientAddr[1] = "test1@adminsystem.net";
recipientAddr[2] = "test2@adminsystem.net";
for( int i = 0; i < 3; i++ )
{
m_oSmtp.ClearRecipient();
m_oSmtp.AddRecipient( recipientAddr[i], recipientAddr[i], 0 );
m_oSmtp.Subject = "test subject";
m_oSmtp.BodyText = "body";
m_oMassSender.Send( m_oSmtp, i, "any" );
}
while( m_oMassSender.GetQueuedCount() > 0 )
System.Threading.Thread.Sleep(50);
while( m_oMassSender.GetCurrentThreads() != m_oMassSender.GetIdleThreads())
System.Threading.Thread.Sleep(50); //wait until all emails have been sent
Console.WriteLine( "all email sent" );
System.Threading.Thread.Sleep(10000);
}
static void OnSent( int lRet,
string ErrDesc,
int nKey,
string tParam,
string Sender,
string Recipients )
{
if( lRet == 0 )
Console.WriteLine("{0} email sent successfully", nKey);
else
Console.WriteLine( "{0} email: {1}", nKey, ErrDesc );
}
}
}
How many threads should I use?
Basically, there is no limitation for worker threads count of MassSender,
it depends on hardware of your machine. We'v tested it with more than 100 threads on Windows XP C800/256M.
Suggestion for worker threads count:
1.Send email via dnslookup 20-50 threads.
2.Send email via IIS SMTP Service 10 threads.
3.Send email via IIS SMTP Pickup 10 threads
A Total Sample
Please refer to CSharpMassMailer sample in ANSMTP installation package.
Free Email Support
Not enough? Please contact our technical support team.
Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered
User)
Remarks
We usually reply emails in 24hours. The reason for getting no response is
likely that your SMTP server bounced our reply. In this case, please try to use
another email address to contact us. Your Hotmail or Yahoo email account is
recommended.
|