ANSMTP Developers Center > Using ANSMTP in Visual Basic

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

Introduction

ANSMTP is a SMTP component which supports all operations of SMTP/ESMTP protocols (RFC 821, RFC 822, RFC 2554). This tutorial covers the basics of sending email with ANSMTP in Visual Basic.

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.

Add reference of ANSMTP to your project

First of all, make sure ANSMTP has been installed on your machine. Now start to create a standard visual basic project: select menu "Project" -> "Reference"s", checked "AOSMTP COMPONENT BUILD ..." and click "OK".

Using ANSMTP in your project

The following code demonstrates how to declare and define objects in your project.

Option Explicit
private m_oSmtp As AOSMTPLib.Mail
					
Sub Form_Load()
  Set m_oSmtp = New AOSMTPLib.Mail
End Sub
					

Although you can create new object instance each time, we recommend to declare object as member variable and create it in Form_Load subroutine so that you can reuse those member variables again without re-create it.

Simple Sample

The following code demonstrates how to send email in synchronous mode

Private Sub SendEmail()
  m_oSmtp.Reset()
  m_oSmtp.FromAddr = "test@adminsystem.net"
  m_oSmtp.ServerAddr = "mail.adminsystem.net"
  'If you don't have a SMTP server, assign "" to ServerAddr,
  'ANSMTP will send email via DNS lookup
  'm_oSmtp.ServerAddr = ""
 
  m_oSmtp.Subject = "test subject"
  m_oSmtp.BodyText = "test body"
  Call m_oSmtp.AddRecipient("Support Team", "support@adminsystem.net", 0)
  If m_oSmtp.SendMail() = 0 Then
    MsgBox "Message delivered"
  Else
    MsgBox m_oSmtp.GetLastErrDescription()
  End If
End Sub

Asynchronous mode and event driving

If you want to use OBJ of ANSMTP in asynchronous mode, you MUST declare it as a member variable in your project with "WithEvents" key word.

Option Explicit
private WithEvents m_oSmtp As AOSMTPLib.Mail
					
Sub Form_Load()
  Set m_oSmtp = New AOSMTPLib.Mail
  m_oSmtp.Asynchronous = 1
End Sub

Then you can handle those events by the following subroutines

Private Sub m_oSmtp_OnAuthenticated()
	'Add your implementation code
End Sub

Private Sub m_oSmtp_OnClosed()
	'Add your implementation code
End Sub

Private Sub m_oSmtp_OnConnected()
	'Add your implementation code
End Sub

Private Sub m_oSmtp_OnError(ByVal lError As Integer, _ 
    ByVal ErrDescription As String)
	'Add your implementation code
End Sub

Private Sub m_oSmtp_OnSending(ByVal lSent As Integer, _
    ByVal lTotal As Integer) 
	'Add your implementation code
End Sub

Note*: You SHOULD NOT invoke SendMail method any more while ansmtp is sending an email, otherwise the email of sending would be terminated automatically.

To send multiple emails in concurrently, you should use FastSender object which enables an inner threading pool to send mass emails with high performance.

Samples

Please refer to VBSAMPLE1, VBSAMPLE2, VBSAMPLE3, VBSAMPL4, VBSAMPL5 and VBMASSMAILER samples 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-2010 © Copyright AdminSystem Software Limited. All rights reserved.   About us  Site Map