Send Email Asynchronously in VB 6.0

In previous section, I introduced how to encrypt email with digital certificate. In this section, I will introduce how to use event handler and send email asynchronously in VB 6.0.

Introduction

Asynchronous mode

In synchronous mode, once SendMail method is called, it returns to application after the method is complete. Therefore, if the runtime (it depends on the networking connection and the email size) is long, your application cannot do anything before this method ends, which results “my application is blocked or halted”. In contrast, in asynchronous mode, as SendMail method works in background, this methods return to application immediately no matter the running method is complete or not.

Event handler

In previous examples, after SendMail method is invoked, if you want to know the progress of the email sending, you should use Event Handler to monitor the progress of email sending.

Note

Remarks: All of samples in this section are based on first section: Send email in a simple VB 6.0 project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[VB 6.0 - Send email with event handler in asynchronous mode - Example]

To demonstrate how to use asynchronous mode and event handler, let’s add a Label control in the form at first, the name of the label is “Label1”.

The following example codes demonstrate how to send email with event handler in asynchronous mode.

Note

To get the full sample projects, please refer to Samples section.

Private WithEvents oSmtp As EASendMailObjLib.Mail
Private m_bError As Boolean
Private m_bFinished As Boolean
Private Sub oSmtp_OnAuthenticated()
    Label1.Caption = "Authenticated"
End Sub
Private Sub oSmtp_OnClosed()
    If Not m_bError Then
        Label1.Caption = "email was sent successfully!"
    End If
    m_bFinished = True
End Sub
Private Sub oSmtp_OnConnected()
    Label1.Caption = "Connected"
End Sub
Private Sub oSmtp_OnError(ByVal lError As Long, ByVal ErrDescription As String)
    Label1.Caption = "failed to send email with error: " & ErrDescription
    m_bError = True
    m_bFinished = True
End Sub
Private Sub oSmtp_OnSending(ByVal lSent As Long, ByVal lTotal As Long)
    Label1.Caption = "Sending " & lSent & "/" & lTotal
End Sub
Private Sub Command1_Click()

    If oSmtp Is Nothing Then
        Set oSmtp = New EASendMailObjLib.Mail
        oSmtp.LicenseCode = "TryIt"
    End If

    m_bError = False
    m_bFinished = False

   ' Set your sender email address
    oSmtp.FromAddr = "test@emailarchitect.net"

    ' Add recipient email address
    oSmtp.AddRecipientEx "support@emailarchitect.net", 0

    ' Set email subject
    oSmtp.Subject = "test email from VB 6.0 in asynchronous mode"

    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project with asynchronous mode"

    ' Your SMTP server address
    oSmtp.ServerAddr = "smtp.emailarchitect.net"

    ' User and password for ESMTP authentication, if your server doesn't require
    ' User authentication, please remove the following codes.
    oSmtp.UserName = "test@emailarchitect.net"
    oSmtp.Password = "testpassword"
    ' If your SMTP server requires SSL connection, please add this line
    'oSmtp.SSL_init

    ' Set asynchronous mode
    oSmtp.Asynchronous = 1

    Command1.Enabled = False
    Label1.Caption = "start to send email ..."

    oSmtp.SendMail

    Do While Not m_bFinished
        ' Wait for the email sending, you can do other thing here
        DoEvents
    Loop

    MsgBox Label1.Caption
    Command1.Enabled = True
End Sub

Next Section

At next section I will introduce how to send mass email with multiple threads in VB6.

Appendix

Comments

If you have any comments or questions about above example codes, please click here to add your comments.