Retrieve Email from IMAP4 server in VB 6.0

In previous section, I introduced retrieving email from POP3 server in VB 6.0. In this section, I will introduce the IMAP4 server.

Introduction

IMAP4 protocol is different with POP3 protocol. First of all, IMAP4 supports retrieving email from different mail folder and folder management. Secondly, IMAP4 supports mail flags management. Therefore, we can do more things with IMAP4 server. To better understand the IMAP4 protocol, please see the following examples.

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 from IMAP4 inbox]

The following example codes demonstrate how to download email from IMAP4 server default mailbox. 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

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

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 = "imap4.emailarchitect.net"
    oServer.User = "test@emailarchitect.net"
    oServer.Password = "testpassword"
    oServer.Protocol = MailServerImap4

    ' Set IMAP4 port
    oServer.Port = 143

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

    On Error GoTo ErrorHandle:
    Dim oClient As New EAGetMailObjLib.MailClient
    oClient.LicenseCode = "TryIt"

    oClient.Connect oServer
    MsgBox "Connected"

    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 IMAP4 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 IMAP4 server.
        oClient.Delete info
    Next

    ' Quit and purge emails marked as deleted from IMAP4 server.
    oClient.Quit
    Exit Sub

ErrorHandle:
    MsgBox Err.Description
End Sub

Because IMAP4 protocol supports folder access, so we can retrieve email from other mailbox rather than default “INBOX”. POP3 protocol doesn’t support this feature.

[VB 6.0 Example - Retrieve email from “Deleted Items”]

The following example codes demonstrate how to retrieve emails from “Deleted Items” in an IMAP4 account. 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

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

Function SearchFolder(ByRef folders, _
    ByVal name As String) As Imap4Folder

    Dim i As Integer

    For i = LBound(folders) To UBound(folders)
        Dim folder As EAGetMailObjLib.Imap4Folder
        Set folder = folders(i)
        MsgBox folder.FullPath

        ' Folder was found.
        If StrComp(folder.name, name, vbTextCompare) = 0 Then
            Set SearchFolder = folder
            Exit Function
        End If

        Set folder = SearchFolder(folder.SubFolders, name)
        If Not (folder Is Nothing) Then
            Set SearchFolder = folder
            Exit Function
        End If
    Next

    ' No folder found
    Set SearchFolder = Nothing
End Function

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 = "imap4.emailarchitect.net"
    oServer.User = "test@emailarchitect.net"
    oServer.Password = "testpassword"
    oServer.Protocol = MailServerImap4

    ' Set IMAP4 port
    oServer.Port = 143

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

    On Error GoTo ErrorHandle:
    Dim oClient As New EAGetMailObjLib.MailClient
    oClient.LicenseCode = "TryIt"

    oClient.Connect oServer
    MsgBox "Connected"

    ' Lookup folder based name.
    Dim folder As Imap4Folder
    Set folder = SearchFolder(oClient.Imap4Folders, "Deleted Items")
    If folder Is Nothing Then
        Err.Raise 2, "ImapFolder", "Folder was not found"
    End If

    ' Select this folder
    oClient.SelectFolder folder

    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 IMAP4 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 IMAP4 server.
        oClient.Delete info
    Next

    ' Quit and purge emails marked as deleted from IMAP4 server.
    oClient.Quit
    Exit Sub

ErrorHandle:
    MsgBox Err.Description
End Sub

Next Section

At next section I will introduce how to retrieve email from Exchange Server with Web Service protocol.

Appendix

Comments

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