Parse Email in VB 6.0

In previous section, I introduced how to download email with background service. In this section, I will introduce how to parse email in VB 6.0.


After you received the emails to the local folder, we can use the following code to parse the email now. The following code demonstrates how to parse from, to, cc, subject, body text, attachments of all emails file received by previous sample.


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 - Parse email]

The following example codes demonstrate how to use EAGetMail POP3 component to parse email sender, to, cc, subject, body text and attachments.


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

Option Explicit

Private Sub ParseEmail(ByVal emlFile As String)
    Dim oMail As New EAGetMailObjLib.Mail
    oMail.LicenseCode = "TryIt"

On Error GoTo ErrorHandle
    oMail.LoadFile emlFile, False

    ' Parse email sender
    MsgBox "From: " & oMail.From.Address

    Dim addressList As EAGetMailObjLib.AddressCollection
    Dim i As Long
    Dim addr As EAGetMailObjLib.MailAddress

    ' Parse email to recipients
    Set addressList = oMail.ToList
    For i = 0 To addressList.Count - 1
        Set addr = addressList.Item(i)
        MsgBox "To: " & addr.Address

    ' Parse email cc
    Set addressList = oMail.CcList
    For i = 0 To addressList.Count - 1
        Set addr = addressList.Item(i)
        MsgBox "Cc: " & addr.Address

    ' Parse email subject
    MsgBox "Subject: " & oMail.Subject

    ' Parse email text body
    MsgBox "Text body: " & oMail.TextBody

    ' Parse email HTML body
    MsgBox "Html body: " & oMail.HtmlBody

    ' Parse attachments
    Dim atts As EAGetMailObjLib.AttachmentCollection
    Dim att As EAGetMailObjLib.Attachment

    Set atts = oMail.AttachmentList
    For i = 0 To atts.Count - 1
        Set att = atts.Item(i)
        MsgBox "Attachment: " &
        ' Save attachment to local file.
        att.SaveAs App.Path & "\inbox\" &, True

    Exit Sub
    MsgBox Err.Description
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 files
    Dim i As Long

    ' Get all *.eml files in specified folder and parse it one by one.
    files = oTools.GetFiles(mailbox & "\*.eml")
    For i = LBound(files) To UBound(files)
        ParseEmail files(i)

    Exit Sub
    MsgBox Err.Description
End Sub

TextBody and HtmlBody

Not every email has both text/plain body text and html body text. However, Mail object provides both TextBody and HtmlBody properties smartly. If the email has only text/plain body, then Mail object converts the text/plain body to html body automatically; if the email has only html body, then Mail object converts the html body to text/plain body automatically.

Next Section

At next section I will introduce how to verify digital signature and decrypt email.



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