<< Click to Display Table of Contents >> Navigation: Samples and Tutorials > ASP.NET - VB > Using AddEmail in ASP.NET / VB projects |
To use AddEmail in your ASP.NET / VB project perform the following steps:
1. Add a reference to the AddEmail library: In Visual Studio main menu, select View -> Solution Explorer to open Solution Explorer. Right-click on the project that will use AddEmail and select Add Reference from the pop-up menu. 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:
Dim objSmtpMail As AddEmailLib.SmtpMailClass
3a. Create an instance of the SmtpMail object:
objSmtpMail = New AddEmailLib.SmtpMailClass()
3b. If you are sending e-mails asynchronously you will need to access the same SmtpMail object on more than one page. In this case store an instance of the SmtpMail object in Session collection:
If IsNothing(Session("SmtpMail")) Then
objSmtpMail = New AddEmailLib.SmtpMailClass()
Session("SmtpMail") = objSmtpMail
Else
objSmtpMail = Session("SmtpMail")
End If
4. Set SMTP server address, port, username and password:
objSmtpMail.SmtpServer = "mail.myserver.com"
objSmtpMail.SmtpPort = 25
objSmtpMail.SmtpUsername = "jsmith"
objSmtpMail.SmtpPassword = "mypassword"
5a. 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
5b. Alternatively, create and fill MailMessage object and send it synchronously using Send or asynchronously using SendAsync:
' 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)
' Save the message number. It is used to retrieve the status of the message
6. If the message was sent asynchronously use GetStatus to check the status of the message:
Dim messageStatus As AddEmailLib.MailStatus
messageStatus = objSmtpMail.GetStatus(messageNumber)
Select Case messageStatus
Case AddEmailLib.MailStatus.MailStatusQueued
...
Case AddEmailLib.MailStatus.MailStatusSending
...
Case AddEmailLib.MailStatus.MailStatusSent
' E-mail was sent successfully
Case AddEmailLib.MailStatus.MailStatusFailed
' E-mail wasn't sent
Dim errorCode As Integer
errorCode = objSmtpMail.GetErrorCode(messageNumber)
Dim errorDescription As String
errorDescription = objSmtpMail.GetErrorDescription(messageNumber)
Case AddEmailLib.MailStatus.MailStatusCanceled
...
End Select
Please refer to the Reference section of this manual for detailed description of AddEmail objects, methods and properties. Included ASP.NET samples provide code snapshots for common operations such as sending text e-mails and sending HTML e-mails.