ANSMTP Developers Center > Threading-Pool in .NET

Note: this article is only suitable for ANSMTP 6.4 or later.
For ANSMTP 6.3 or earlier version, please click here.

Important notice:* For C#, Visual Basic.NET, Managed C++, J#, JScript.NET and ASP.NET developers, we strongly recommend that you use the pure .NET class EASendMail SMTP Component instead of ANSMTP SMTP Component.

Introduction

To enhance the performance in sending mass mail, FastSender Object, which is designed based on multi-threading algorithm, is added to ANSMTP. This tutorial shows how to use FastSender in your application.

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?

 

FastSender has an inner threading pool based on MaxThreads count. Firstly, Send or SendByPickup submits email to FastSender 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 FastSender 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 FastSender object to send mass emails.

using System;
using AOSMTPLib;

namespace FastSenderConsole
{
  class FastSender
  {
    private static FastSenderClass m_oFastSender = null;
    private static MailClass m_oSmtp = null;

    [STAThread]
    static void Main(string[] args)
    {
      string []recipientAddr = new string[3];
      if( m_oFastSender == null ||  m_oSmtp == null )
      {
        m_oFastSender = new FastSenderClass();
        m_oSmtp = new MailClass();
        m_oFastSender.MaxThreads = 10; //set the maximum worker threads
        bind OnSent event to OnSent method
        _IFastSenderEvents_OnSentEventHandler OnSentEventHandler = 
                        new _IFastSenderEvents_OnSentEventHandler(OnSent);
        m_oFastSender.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:
      //FastSender 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_oFastSender.Send( m_oSmtp, i, "any" );
      }

      while( m_oFastSender.GetQueuedCount() > 0 )
        System.Threading.Thread.Sleep(50);
              
      while( m_oFastSender.GetCurrentThreads() != m_oFastSender.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 FastSender, 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.



2001-2008 © Copyright AdminSystem Software Limited. All rights reserved.   About us  Site Map