Retrieve Email from Exchange Server with WebDAV protocol in VB 6.0

In previous section, I introduced how to retrieve email from Exchange Server with Web Service (EWS) protocol in VB 6.0. In this section, I will introduce the WebDAV protocol (Exchange 2000/2003).

Introduction

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

Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007 or later version, POP3/IMAP4 service is disabled by default. If you don’t want to use POP3/IMAP4 to download email from Exchange Server, you can use Exchange Web Service (Exchange 2007/2010 or later version) or WebDAV (Exchange 2000/2003) protocol.

For Exchange Server 2007 or later version, WebDAV service is also disabled by default, so please use Exchange Web Service (EWS) instead of WebDAV protocol.

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 Exchange Inbox]

The following example codes demonstrate how to use EAGetMail to download email from Exchange server default mailbox using WebDAV protocol. 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

    ' Please use domain\user as the user name
    Dim oServer As New EAGetMailObjLib.MailServer
    oServer.Server = "exch.emailarchitect.net"

    oServer.User = "emailarchitect.net\test"
    oServer.Password = "testpassword"

    oServer.Protocol = MailServerDAV

    ' If your Exchange WebDAV server requires SSL connection,
    ' Please add the following codes:
    ' oServer.SSLConnection = True

    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 Exchange 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

        ' Delete email from Exchange server.
        oClient.Delete info
    Next

    ' Quit from Exchange server.
    oClient.Quit
    Exit Sub

ErrorHandle:
    MsgBox Err.Description
End Sub

Because Exchange WebDAV 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 Exchange account.

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

    ' Please use domain\user as the user name
    Dim oServer As New EAGetMailObjLib.MailServer
    oServer.Server = "exch.emailarchitect.net"

    oServer.User = "emailarchitect.net\test"
    oServer.Password = "testpassword"

    oServer.Protocol = MailServerDAV

    ' If your Exchange WebDAV server requires SSL connection,
    ' Please add the following codes:
    ' oServer.SSLConnection = True

    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 Exchange 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

        ' Delete email from Exchange server.
        oClient.Delete info
    Next

    ' Quit from Exchange server.
    oClient.Quit
    Exit Sub

ErrorHandle:
    MsgBox Err.Description
End Sub

Next Section

At next section I will introduce how to retrieve email over SSL connection.

Appendix

Comments

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