VB6 - Convert email to HTML

The following vb6 codes demonstrate how to convert email to a HTML page and display it using Web browser Control.

After the email was converted to HTML page, you can browse it with web browser. You can get everything in the HTML page such as From, To, Cc, Subject, Date, Attachments and Embedded images.

Installation

Before you can use the following sample codes, you should download the EAGetMail Installer and install it on your machine at first. Full sample projects are included in this installer.

Add reference

To use EAGetMail POP3 & IMAP4 ActiveX Object in your project, the first step is Add reference of EAGetMail to your project. Please go to menu -> Project -> References -> and select EAGetMailObj ActiveX Object, click OK, the reference will be added to your project, and you can start to use it to retrieve email and parse email in your project.

add reference in VB6

Add reference in VBA (EXCEL/Access/Outlook)

Open VBA IDE by press Alt+F11, Please select menu -> Tools -> References -> and select EASendMailObj ActiveX Object, click OK, the reference will be added to current VBA project, and you can start to use it to retrieve email and parse email in your VBA project.

To better demonstrate how to retrieve email and parse email, let’s create a VB 6.0 Standard EXE project at first, then add a CommandButton on the Form, double-click this button. It is like this

VB 6.0 standard project

VB6 - Parse and convert email to HTML - example

The following example codes demonstrate converting email to HTML page. In order to run it correctly, please change email server, user, password, folder, file name value to yours.

Note

To get full sample projects, please download and install EAGetMail on your machine.

Option Explicit

Const CRYPT_MACHINE_KEYSET = 32
Const CRYPT_USER_KEYSET = 4096
Const CERT_SYSTEM_STORE_CURRENT_USER = 65536
Const CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072

Private oTools As New EAGetMailObjLib.Tools

' We generate a html + attachment folder for every email, once the html is create,
' Next time we don't need to parse the email again.
Private Sub GenerateHtmlForEmail(ByVal htmlName As String, _
    ByVal emlFile As String, ByVal tempFolder As String)

On Error GoTo ErrorGenHtml

    Dim oTools As New EAGetMailObjLib.Tools
    Dim oMail As New EAGetMailObjLib.Mail
    oMail.LicenseCode = "TryIt"

    oMail.LoadFile emlFile, False
    If Err.Number <> 0 Then
        MsgBox Err.Description
        Exit Sub
    End If

    On Error Resume Next
    If oMail.IsEncrypted Then
        Set oMail = oMail.Decrypt(Nothing)
        If Err.Number <> 0 Then
            MsgBox Err.Description
        End If
    End If

    If oMail.IsSigned Then
        oMail.VerifySignature
        If Err.Number <> 0 Then
            MsgBox Err.Description
        End If
    End If

On Error GoTo ErrorGenHtml

    ' Decode winmail.dat (TNEF) and RTF body automatically
    ' also convert RTF body to HTML automatically.
    oMail.DecodeTNEF

    Dim html As String
    html = oMail.HtmlBody

    Dim hdr As String
    hdr = hdr & "<font face=""Courier New,Arial"" size=2>"
    hdr = hdr & "<b>From:</b> " + FormatHtmlTag(oMail.From.name & "<" & _
    oMail.From.Address & ">") + "<br>"

    Dim i, addrs
    Set addrs = oMail.ToList

    If (addrs.Count > 0) Then
        hdr = hdr & "<b>To:</b> "
        For i = 0 To addrs.Count - 1
            hdr = hdr & FormatHtmlTag(addrs.Item(i).name & "<" & addrs.Item(i).Address & ">")
            hdr = hdr & ";"
        Next
        hdr = hdr & "<br>"
    End If

    Set addrs = oMail.CcList

    If (addrs.Count > 0) Then
        hdr = hdr & "<b>Cc:</b> "
        For i = 0 To addrs.Count - 1
            hdr = hdr & FormatHtmlTag(addrs.Item(i).name & "<" & addrs.Item(i).Address & ">")
            hdr = hdr & ";"
        Next
        hdr = hdr & "<br>"
    End If

    hdr = hdr & "<b>Subject:</b>" & FormatHtmlTag(oMail.Subject) & "<br>" & vbCrLf

    Dim atts
    Set atts = oMail.AttachmentList
    If (atts.Count > 0) Then

        If Not oTools.ExistFile(tempFolder) Then
            oTools.CreateFolder (tempFolder)
        End If

        hdr = hdr & "<b>Attachments:</b>"
        For i = 0 To atts.Count - 1
            Dim att As Attachment
            Set att = atts.Item(i)

            Dim attname
            attname = tempFolder & "\" & att.name
            att.SaveAs attname, True

            hdr = hdr & "<a href=""" & attname & """ target=""_blank"">" & att.name & "</a> "
            If Len(att.ContentID) > 0 Then
                'show embedded image.
                html = Replace(html, "cid:" + att.ContentID, attname)
            ElseIf InStr(1, att.ContentType, "image/", vbTextCompare) = 1 Then
                'show attached image.
                html = html & "<hr><img src=""" & attname & """>"
            End If
        Next
    End If

    hdr = "<meta HTTP-EQUIV=""Content-Type"" Content=""text-html; charset=utf-8"">" & hdr
    html = hdr & "<hr>" & html
    oTools.WriteTextFile htmlName, html, 65001

    MsgBox emlFile & " has been converted to html successfully!"
    Exit Sub

ErrorGenHtml:
    MsgBox "Failed to generate html file for the email; " & Err.Description

End Sub

Private Function FormatHtmlTag(ByVal src As String) As String
    src = Replace(src, ">", "&gt;")
    src = Replace(src, "<", "&lt;")
    FormatHtmlTag = src
End Function

Private Sub ConvertMailToHtml(ByVal fileName As String)
    Dim pos
    pos = InStrRev(fileName, ".")
    Dim mainName
    Dim htmlName
    mainName = Mid(fileName, 1, pos - 1)
    htmlName = mainName & ".htm"

    Dim tempFolder As String
    tempFolder = mainName
    If Not (oTools.ExistFile(htmlName)) Then
        ' We haven't generate the html for this email, generate it now.
        GenerateHtmlForEmail htmlName, fileName, tempFolder
    End If

End Sub

Private Sub Command1_Click()

    ConvertMailToHtml "c:\my folder\test.eml"

End Sub

32bit/x64 ActiveX DLL

Seperate builds of run-time dll for 32 and x64 platform

File Platform
Installation Path\Lib\native\x86\EAGetMailObj.dll 32 bit
Installation Path\Lib\native\x64\EAGetMailObj.dll 64 bit

Distribution

  • Standard EXE

    For VB6, C++, Delphi or other standard exe application, you can distribute EAGetMailObj.dll with your application to target machine without COM-registration and installer. To learn more detail, please have a look at Registration-free COM with Manifest File.

  • Script

    For ASP, VBScript, VBA, MS SQL Stored Procedure, you need to install EAGetMail on target machine by EAGetMail installer, both 32bit/x64 DLL are installed and registered.

Appendix

Comments

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