<< Click to Display Table of Contents >> Navigation: Samples and Tutorials > ASP - VBScript > Using AddEmail in ASP / VBScript projects |
To use AddEmail in your ASP / VBScript project perform the following steps:
1. (Optional) If you are using Visual Interdev add a reference to the AddEmail library: In Visual Interdev main menu, select Project -> References. Find and select AddEmail 4.0 Type Library.
2. Declare variable that will hold a reference to the SmtpMail object:
Dim objSmtpMail
3a. Create an instance of the SmtpMail object:
Set objSmtpMail = Server.CreateObject("AddEmail.SmtpMail")
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 IsEmpty(Session("SmtpMail")) Then
Set objSmtpMail = Server.CreateObject("AddEmail.SmtpMail")
Set Session("SmtpMail") = objSmtpMail
Else
Set 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 SimpleSendScriptable or SimpleSendAttachmentScriptable:
Dim strError, numResultCode
numResultCode = objSmtpMail.SimpleSendScriptable("jsmith@myserver.com", "jane@someserver.com;james@someserver.com", "test", "Test message", strError)
If numResultCode = 0 Then
' The email was sent successfully
Response.Write "Sent successfully!"
Else
' The email was not sent, strError contains error description
Response.Write strError
End If
5b. Alternatively, create and fill MailMessage object and send it synchronously using SendScriptable or asynchronously using SendAsync:
Dim objMailMessage, objMailAttachment, objMailAddress, strError, numResultCode
' Create message and setup subject and body
Set objMailMessage = CreateObject("AddEmail.MailMessage")
objMailMessage.MessageBodyFormat = 1 'HTML format
objMailMessage.MessageSubject = "test"
objMailMessage.MessageBody = "<html><body><b>Testing...</b></body></html>"
' Add first attachment
Set objMailAttachment = CreateObject("AddEmail.MailAttachment")
objMailAttachment.File = "c:\files\doc1.pdf"
objMailMessage.AddAttachment(objMailAttachment)
' Add second attachment
Set objMailAttachment = CreateObject("AddEmail.MailAttachment")
objMailAttachment.File = "c:\files\doc2.pdf"
objMailMessage.AddAttachment(objMailAttachment)
' Setup sender
Set objMailAddress = CreateObject("AddEmail.MailAddress")
objMailAddress.Name = "John Smith"
objMailAddress.Address = "jsmith@myserver.com"
objMailMessage.Sender = objMailAddress
' Setup first recipient
Set objMailAddress = CreateObject("AddEmail.MailAddress")
objMailAddress.Name = "Jane Smith"
objMailAddress.Address = "jane@someserver.com"
objMailMessage.AddRecipient(objMailAddress)
' Setup second recipient
Set objMailAddress = CreateObject("AddEmail.MailAddress")
objMailAddress.Name = "James Smith"
objMailAddress.Address = "james@someserver.com"
objMailMessage.AddRecipient(objMailAddress)
' Send prepared message synchronously
Dim strError, numResultCode
numResultCode = objSmtpMail.SendScriptable(objMailMessage, True, strError)
If numResultCode = 0 Then
' The email was sent successfully
Response.Write "Sent successfully!"
Else
' The email was not sent, strError contains error description
Response.Write strError
End If
' Alternatively, send prepared message asynchronously
'Dim messageNumber
'messageNumber = objSmtpMail.SendAsync(objMailMessage, 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 numStatus
numStatus = objSmtpMail.GetStatus(numMessageNumber)
Select Case numStatus
Case 0 'MailStatusQueued
Response.Write "Queued"
Case 1 'MailStatusSending
Response.Write "Sending..."
Case 2 'MailStatusSent
Response.Write "Sent successfully!"
Case 3 'MailStatusFailed
Response.Write "Error " & objSmtpMail.GetErrorCode(numMessageNumber)
Response.Write "<BR>" & objSmtpMail.GetErrorDescription(numMessageNumber)
Case 4 'MailStatusCanceled
Response.Write "Message canceled"
End Select
Please refer to the Reference section of this manual for detailed description of AddEmail objects, methods and properties. Included ASP samples provide code snapshots for common operations such as sending text e-mails and sending HTML e-mails.