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

' 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)
    Label1.Caption = "Authorized"
End Sub

Private Sub oClient_OnConnected(ByVal oSender As Object, Cancel As Boolean)
    Label1.Caption = "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)
    Label1.Caption = "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
    Label1.Caption = "Receiving " & info.Index & ", " & Received & "/" & Total & "..."
End Sub

Private Sub oClient_OnSecuring(ByVal oSender As Object, Cancel As Boolean)
    Label1.Caption = "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

    ' If your POP3 requires SSL connection
    ' Please add the following codes
    ' oServer.SSLConnection = True
    ' oServer.Port = 995

    On Error GoTo ErrorHandle:

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

    oClient.Connect oServer

    Dim infos
    infos = oClient.GetMailInfos()
    MsgBox UBound(infos) + 1 & " emails"

    Dim i As Integer
    For i = LBound(infos) To UBound(infos)
        Dim info As EAGetMailObjLib.MailInfo
        Set info = infos(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 purge 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.