Retrieve Email with Event Handler in VB 6.0

In previous section, I introduced how to retrieve email from Hotmail/MSN Live account. In this section, I will introduce how to retrieve email with event handler in VB 6.0.

Introduction

After Connect method, GetMail method or other methods are invoked, if you want to know the progress of the email receiving, you should use Event Handler. The following sample codes demonstrate how to use Event Handler to monitor the progress of email receiving.

Note

Remarks: All of examples in this section are based on first section: 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 to your project.

[VB 6.0 Example - Retrieve email with event handler]

The following example codes demonstrate how to use EAGetMail POP3 component to retrieve email with event handler. In order to run it correctly, please change email server, user, password, folder, file name values.

Note

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

Option Explicit
Private WithEvents oClient As EAGetMailObjLib.MailClient

Const MailServerPop3 = 0
Const MailServerImap4 = 1
Const MailServerEWS = 2
Const MailServerDAV = 3
Const MailServerMsGraph = 4

' Catching the following events is not necessary,
' just make the application more user friendly.
' If you use the object in asp.net/windows service or non-gui application,
' You need not to catch the following events.
' To learn more detail, please refer to the code in EAGetMail EventHandler
Private Sub oClient_OnAuthorized(ByVal oSender As Object, Cancel As Boolean)
    Debug.Print "Authorized"
End Sub

Private Sub oClient_OnConnected(ByVal oSender As Object, Cancel As Boolean)
    Debug.Print "Connected"
End Sub

Private Sub oClient_OnIdle(ByVal oSender As Object, Cancel As Boolean)
    DoEvents
End Sub

Private Sub oClient_OnQuit(ByVal oSender As Object, Cancel As Boolean)
    Debug.Print "Quit"
End Sub

Private Sub oClient_OnReceivingDataStream(ByVal oSender As Object, _
ByVal oInfo As Object, ByVal Received As Long, ByVal Total As Long, Cancel As Boolean)
    Dim info As EAGetMailObjLib.MailInfo
    Set info = oInfo
    Debug.Print "Receiving " & info.Index & ", " & Received & "/" & Total & "..."
End Sub

Private Sub oClient_OnSecuring(ByVal oSender As Object, Cancel As Boolean)
    Debug.Print "Securing ..."
End Sub

Private Sub Command1_Click()
    Dim curpath As String
    Dim mailbox As String
    Dim oTools As New EAGetMailObjLib.Tools

    ' Create a folder named "inbox" under current directory
    ' to save the email retrieved.
    curpath = App.Path
    mailbox = curpath & "\inbox"
    oTools.CreateFolder mailbox

    Dim oServer As New EAGetMailObjLib.MailServer
    oServer.Server = "pop3.emailarchitect.net"
    oServer.User = "test@emailarchitect.net"
    oServer.Password = "testpassword"
    oServer.Protocol = MailServerPop3

    ' Enable SSL/TLS connection, most modern email servers require SSL/TLS by default
    oServer.SSLConnection = True
    oServer.Port = 995

    ' If your POP3 doesn't deploy SSL connection
    ' Please use
    ' oServer.SSLConnection = False
    ' oServer.Port = 110

On Error GoTo ErrorHandle:

    If oClient Is Nothing Then
        Set oClient = New EAGetMailObjLib.MailClient
        oClient.LicenseCode = "TryIt"
    End If

    oClient.Connect oServer

    Dim infos As EAGetMailObjLib.MailInfoCollection
    Set infos = oClient.GetMailInfoList()
    MsgBox infos.Count & " emails"

    Dim i As Long
    For i = 0 To infos.Count - 1
        Dim info As EAGetMailObjLib.MailInfo
        Set info = infos.Item(i)

        MsgBox "Index: " & info.Index & "; Size: " & info.Size & _
        "; UIDL: " & info.UIDL

        ' Receive email from POP3 server
        Dim oMail As EAGetMailObjLib.Mail
        Set oMail = oClient.GetMail(info)

        MsgBox "From: " & oMail.From.Address & _
            vbCrLf & "Subject: " & oMail.Subject

        Dim fileName As String
        ' Generate a random file name by current local datetime,
        ' You can use your method to generate the filename if you do not like it
        fileName = mailbox & "\" & oTools.GenFileName(i) & ".eml"

        ' Save email to local disk
        oMail.SaveAs fileName, True

        ' Mark email as deleted from POP3 server.
        oClient.Delete info
    Next

    ' Quit and expunge emails marked as deleted from POP3 server.
    oClient.Quit
    Exit Sub

ErrorHandle:
    MsgBox Err.Description
End Sub

Next Section

At next section I will introduce how to use UIDL function to mark the email has been downloaded.

Appendix

Comments

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