ANSMTP Developers Center > Using ANSMTP in Visual Basic.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
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.NET.
How to declare and define objects;
How to send email in synchronous mode;
How to send email in asynchronous mode;
How to send email with SMTP server;
How to send email without specified SMTP server (via DNS lookup).
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 in your project
First of all, make sure ANSMTP has been installed on your machine. Then create a Windows Form project in Visual Studio.NET: select menu "Project" -> "Add Reference" -> COM", and select "AOSMTP COMPONENT BUILD ...".
Declare and define objects
Imports AOSMTPLib .... private m_oSmtp As New AOSMTPLib.MailClass()
Although you can create new object instance each time, we recommend to declare object as member variable so that you just need to create it once and then enjoy reuse those member variables without re-create it again.
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"
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 OBJClass in asynchronous mode, you MUST declare it as a member variable in your project with "WithEvents" key word.
Imports AOSMTPLib ... Private WithEvents m_oSmtp As New AOSMTPLib.MailClass()
Then you can handle those events by the following subroutines
Private Sub m_oSmtp_OnAuthenticated() Handles m_oSmtp.OnAuthenticated
'Add your implementation code
End Sub
Private Sub m_oSmtp_OnClosed() Handles m_oSmtp.OnClosed
'Add your implementation code
End Sub
Private Sub m_oSmtp_OnConnected() Handles m_oSmtp.OnConnected
'Add your implementation code
End Sub
Private Sub m_oSmtp_OnError(ByVal lError As Integer, _
ByVal ErrDescription As String) Handles m_oSmtp.OnError
'Add your implementation code
End Sub
Private Sub m_oSmtp_OnSending(ByVal lSent As Integer, _
ByVal lTotal As Integer) Handles m_oSmtp.OnSending
'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.
A Total Sample
Please refer to VBDOTNET 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.
|