Retrieve Email over SSL connection in VB 6.0

In previous section, I introduced how to retrieve email from Exchange Server with WebDAV protocol. In this section, I will introduce how to retrieve email over SSL connection in VB 6.0.

Introduction

SSL and TLS

SSL connection encrypts data between the email component and POP3 server or IMAP4 server to protect user, password and email content in TCP/IP level. Now this technology is commonly used and many email servers are deployed with SSL such as Gmail, Yahoo and Hotmail.

There are two ways to deploy SSL on email server:

  • Implicit SSL

    Deploying SSL on another port (POP3: 995 port or IMAP4: 993 port) directly. This is most common way.

  • Explicit SSL (TLS)

    Using STARTTLS or STLS command to switch SSL channel on normal port (POP3: 110 port or IMAP4: 143 port);

EAGetMail POP3 component supports both ways. Please see the following example codes:

For Exchange Web Service/WebDAV protocol, the SSL is based on HTTPS connection, so you just need to set SSLConnection property and simply ignore Port property. Notice: Exchange Web Service requires SSL connection by default.

[VB 6.0 Example - SSL/TLS]

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

Const ConnectSSLAuto = 0
Const ConnectSSL = 1
Const ConnectTLS = 2

' Retrieve email by normal TCP/IP without SSL connection
' POP3
Dim oServer As New EAGetMailObjLib.MailServer
oServer.Server = "pop3.emailarchitect.net"
oServer.User = "test@emailarchitect.net"
oServer.Password = "testpassword"
oServer.Protocol = MailServerPop3
oServer.Port = 110

' IMAP4
Dim oServer As New EAGetMailObjLib.MailServer
oServer.Server = "imap4.emailarchitect.net"
oServer.User = "test@emailarchitect.net"
oServer.Password = "testpassword"
oServer.Protocol = MailServerImap4
oServer.Port = 143

' Retrieve email over SSL connection with direct SSL.
' POP3 SSL
Dim oServer As New EAGetMailObjLib.MailServer
oServer.Server = "pop3.emailarchitect.net"
oServer.User = "test@emailarchitect.net"
oServer.Password = "testpassword"
oServer.Protocol = MailServerPop3
oServer.SSLConnection = True
oServer.Port = 995

' IMAP4 SSL
Dim oServer As New EAGetMailObjLib.MailServer
oServer.Server = "imap4.emailarchitect.net"
oServer.User = "test@emailarchitect.net"
oServer.Password = "testpassword"
oServer.Protocol = MailServerImap4
oServer.SSLConnection = True
oServer.Port = 993

' Retrieve email by SSL connection with STARTTLS or TLS command switching
' POP3 TLS
Dim oServer As New EAGetMailObjLib.MailServer
oServer.Server = "pop3.emailarchitect.net"
oServer.User = "test@emailarchitect.net"
oServer.Password = "testpassword"
oServer.Protocol = MailServerPop3
oServer.SSLConnection = True
oServer.Port = 110
oServer.SSLType = ConnectTLS

' IMAP4 STARTTLS
Dim oServer As New EAGetMailObjLib.MailServer
oServer.Server = "imap4.emailarchitect.net"
oServer.User = "test@emailarchitect.net"
oServer.Password = "testpassword"
oServer.Protocol = MailServerImap4
oServer.SSLConnection = True
oServer.Port = 143
oServer.SSLType = ConnectTLS

' Exchange WebDAV
Dim oServer As New EAGetMailObjLib.MailServer
oServer.Server = "exch.emailarchitect.net"
oServer.User = "emailarchitect.net\test"
oServer.Password = "testpassword"
oServer.Protocol = MailServerDAV

' Exchange Web Service SSL
Dim oServer As New EAGetMailObjLib.MailServer
oServer.Server = "exch.emailarchitect.net"
oServer.User = "emailarchitect.net\test"
oServer.Password = "testpassword"
oServer.Protocol = MailServerEWS
oServer.SSLConnection = True

' Exchange WebDAV SSL
Dim oServer As New EAGetMailObjLib.MailServer
oServer.Server = "exch.emailarchitect.net"
oServer.User = "emailarchitect.net\test"
oServer.Password = "testpassword"
oServer.Protocol = MailServerDAV
oServer.SSLConnection = True

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 POP3 server over SSL on 995 port]

The following example codes demonstrate how to retrieve emails from POP3 server over SSL connection on 995 port. 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 = "pop3.emailarchitect.net"
    oServer.User = "test@emailarchitect.net"
    oServer.Password = "testpassword"
    oServer.Protocol = MailServerPop3

    ' Enable SSL Connection
    oServer.SSLConnection = True

    ' Set 995 SSL Port
    oServer.Port = 995

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

[VB 6.0 Example - Retrieve email from IMAP4 server over SSL on 993 port]

The following example codes demonstrate how to retrieve emails from IMAP4 server over SSL connection on 993 port. 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 = "imap.emailarchitect.net"
    oServer.User = "test@emailarchitect.net"
    oServer.Password = "testpassword"
    oServer.Protocol = MailServerImap4

    ' Enable SSL Connection
    oServer.SSLConnection = True

    ' Set 993 IMAP4 SSL Port
    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

Next Section

At next section I will introduce how to download emails from Gmail account.

Appendix

Comments

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