<< Click to Display Table of Contents >> Navigation: Samples and Tutorials > VB.NET > Using AddEmail in VB.NET projects |
To use AddEmail in your Visual Basic .NET project perform the following steps:
1. Add a reference to the AddEmail library: In Visual Studio main menu, select Project -> Add Reference. Click COM tab, find and select AddEmail 4.0 Type Library, click Select button then OK button.
2. Declare a variable that will hold a reference to the SmtpMail object and create an instance:
Dim WithEvents objSmtpMail As New AddEmailLib.SmtpMailClass()
3. Set SMTP server address, port, username and password:
objSmtpMail.SmtpServer = "mail.myserver.com"
objSmtpMail.SmtpPort = 25
objSmtpMail.SmtpUsername = "jsmith"
objSmtpMail.SmtpPassword = "mypassword"
4a. Send an e-mail synchronously using SimpleSend or SimpleSendAttachment:
Dim strError As String
Dim resultCode As Integer
resultCode = objSmtpMail.SimpleSend("jsmith@myserver.com", "jane@someserver.com;james@someserver.com", "test", "Test message", strError)
If resultCode = 0 Then
' E-mail was sent successfully
Else
' Send failed, strError contains detailed error description
End If
4b. Alternatively, create and fill MailMessage object and send it synchronously using Send or asynchronously using SendAsync method:
' Create message and setup subject and body
Dim objMessage As New AddEmailLib.MailMessageClass()
objMessage.MessageSubject = "test"
objMessage.MessageBody = "Test message"
' Setup sender
Dim objSender As New AddEmailLib.MailAddressClass()
objSender.Address = "jsmith@myserver.com"
objSender.Name = "John Smith"
objMessage.Sender = objSender
' Setup first recipient
Dim objRecipient As AddEmailLib.MailAddressClass
objRecipient = New AddEmailLib.MailAddressClass()
objRecipient.Address = "jane@someserver.com"
objRecipient.Name = "Jane Smith"
objMessage.AddRecipient(objRecipient)
' Setup second recipient
objRecipient = New AddEmailLib.MailAddressClass()
objRecipient.Address = "james@someserver.com"
objRecipient.Name = "James Smith"
objMessage.AddRecipient(objRecipient)
' Send prepared message synchronously
Dim strError As String
Dim resultCode As Integer
resultCode = objSmtpMail.Send(objMessage, True, strError)
If resultCode = 0 Then
' E-mail was sent successfully
Else
' Send failed, strError contains detailed error description
End If
' Alternatively, send prepared message asynchronously
'Dim messageNumber As Integer
'messageNumber = objSmtpMail.SendAsync(objMessage, True)
5. If the e-mail is sent asynchronously your application will need to process events to find out when the email was sent successfully or send operation failed. Declare event handlers to process events from the SmtpMail object :
Private Sub OnStatusChange(ByVal messageNumber As Integer, ByVal newStatus As Integer) Handles objSmtpMail.OnStatusChange
' Event processing code
End Sub
Private Sub OnProgress(ByVal messageNumber As Integer, ByVal bytesSent As Integer, ByVal bytesTotal As Integer) Handles objSmtpMail.OnProgress
' Event processing code
End Sub
Please refer to the Reference section of this manual for detailed description of AddEmail objects, methods and properties. Included Visual Basic .NET samples provide code snapshots for common operations such as sending text e-mails, sending HTML e-mails, adding attachments to e-mails, creating HTML e-mails with embedded images.