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.

Introduction

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.

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

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

Note

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 arAddr
    Dim i As Integer
    Dim addr As EAGetMailObjLib.MailAddress

    ' Parse email to recipients
    arAddr = oMail.To
    For i = LBound(arAddr) To UBound(arAddr)
        Set addr = arAddr(i)
        MsgBox "To: " & addr.Address
    Next

    ' Parse email cc
    arAddr = oMail.Cc
    For i = LBound(arAddr) To UBound(arAddr)
        Set addr = arAddr(i)
        MsgBox "Cc: " & addr.Address
    Next

    ' 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

    Dim atts
    Dim att As EAGetMailObjLib.Attachment

    ' Parse attachments
    atts = oMail.Attachments
    For i = LBound(atts) To UBound(atts)
        Set att = atts(i)
        MsgBox "Attachment: " & att.name
        ' Save attachment to local file.
        att.SaveAs( App.Path & "\inbox\" & att.name, true )
    Next

    Exit Sub
ErrorHandle:
    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)
    Next

    Exit Sub
ErrorHandle:
    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.

Appendix

Comments

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