email component

ANSMTP Developers Center > Threading-Pool in Visual Basic

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.

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 object provides an easy way to send mass emails with high performance.

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.

Option Explicit

Private WithEvents m_oMassSender As ANSMTPLib.MassSender
Private m_oSmtp As ANSMTPLib.OBJ

Private sub SendEmail()
  Dim recipientAddr(3) As String
  Dim i As Integer
  
  If m_oMassSender Is Nothing Or m_oSmtp Is Nothing Then
    Set m_oMassSender = New ANSMTPLib.MassSender
    Set m_oSmtp = New ANSMTPLib.OBJ
    m_oMassSender.MaxThreads = 10 'set the maximum no. of worker threads
  End If
  
  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 i = 0 To 2
    m_oSmtp.ClearRecipient
    m_oSmtp.AddRecipient recipientAddr(i), recipientAddr(i), 0
    m_oSmtp.Subject = "test subject"
    m_oSmtp.BodyText = "test body"
    Call m_oMassSender.Send( m_oSmtp, i, "any" )
  Next
  
End Sub

Private Sub m_oMassSender_OnSent(ByVal lRet As Long, _
                                 ByVal ErrDesc As String, _
                                 ByVal nKey As Long, _ 
                                 ByVal tParam As String,  _
                                 ByVal Sender As String, _ 
                                 ByVal Recipients As String)
  If lRet = 0 Then
    MsgBox nKey & " email was sent successfully"
  Else
    MsgBox nKey & ": " & ErrDesc
  End If
End Sub

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 VBMassMailer 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-2011 © Copyright AdminSystem Software Limited. All rights reserved.   About us  Site Map                       Follow emailarchitect on Twitter