Using AddEmail in VB6 projects

<< Click to Display Table of Contents >>

Navigation:  Samples and Tutorials > VB6 >

Using AddEmail in VB6 projects

 

To use AddEmail in your Visual Basic 6 project perform the following steps:

 

1. Add a reference to the AddEmail library: In VB6 main menu, select Project -> References. Find and select AddEmail 4.0 Type Library.

 

2. Declare a variable that will hold a reference to the SmtpMail object:

 

Dim WithEvents objSmtpMail As AddEmailLib.SmtpMail

 

3. Create an instance of SmtpMail object in Form_Load() or other functions:

 

Set objSmtpMail = New AddEmailLib.SmtpMail

 

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 Long

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.MailMessage

objMessage.MessageSubject = "test"

objMessage.MessageBody = "Test message"

 

' Setup sender

Dim objSender As New AddEmailLib.MailAddress

objSender.Address = "jsmith@myserver.com"

objSender.Name = "John Smith"

objMessage.Sender = objSender

 

' Setup first recipient

Dim objRecipient As AddEmailLib.MailAddress

Set objRecipient = New AddEmailLib.MailAddress

objRecipient.Address = "jane@someserver.com"

objRecipient.Name = "Jane Smith"

objMessage.AddRecipient objRecipient

 

' Setup second recipient

Set objRecipient = New AddEmailLib.MailAddress

objRecipient.Address = "james@someserver.com"

objRecipient.Name = "James Smith"

objMessage.AddRecipient objRecipient

 

' Send prepared message synchronously

Dim strError As String

Dim resultCode As Long

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 Long

'messageNumber = objSmtpMail.SendAsync(objMessage, True)

 

6. 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. To process events from the SmtpMail object declare event handlers as shown below:

 

Private Sub objSmtpMail_OnStatusChange(ByVal messageNumber As Long, ByVal newStatus As Long)

  ' Event processing code

End Sub

 

Private Sub objSmtpMail_OnProgress(ByVal messageNumber As Long, ByVal bytesSent As Long, ByVal bytesTotal As Long)

  ' 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 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.