VB.NET - Convert email to HTML

The following vb.net 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.


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.

Install from NuGet

You can also install the run-time assembly by NuGet. Run the following command in the NuGet Package Manager Console:

Install-Package EAGetMail


If you install it by NuGet, no sample projects are installed, only .NET assembly is installed.

Add reference

To use EAGetMail POP3 & IMAP Component in your project, the first step is “Add reference of EAGetMail to your project”. Please create or open your project with Visual Studio, then go to menu -> Project -> Add Reference -> .NET -> Browse..., and select Installation path\Lib\[netversion]\EAGetMail.dll, click Open-> OK, the reference will be added to the project, you can start to use it to retrieve email and parse email in your project.

add reference in c#/vb.net/managed c++/cli

.NET assembly

Because EAGetMail has separate builds for .Net Framework, please refer to the following table and choose the correct dll.

Separate builds of run-time assembly for .Net Framework 2.0, 4.0, 4.5, 4.6.1, 4.7.2, 4.8.1, .NET 6.0, NET 7.0, .NET 8.0, .NET Standard 2.0 and .Net Compact Framework 2.0, 3.5.

File .NET Framework Version
Lib\net20\EAGetMail.dll Built with .NET Framework 2.0
It requires .NET Framework 2.0, 3.5 or later version.
Lib\net40\EAGetMail.dll Built with .NET Framework 4.0
It requires .NET Framework 4.0 or later version.
Lib\net45\EAGetMail.dll Built with .NET Framework 4.5
It requires .NET Framework 4.5 or later version.
Lib\net461\EAGetMail.dll Built with .NET Framework 4.6.1
It requires .NET Framework 4.6.1 or later version.
Lib\net472\EAGetMail.dll Built with .NET Framework 4.7.2
It requires .NET Framework 4.7.2 or later version.
Lib\net481\EAGetMail.dll Built with .NET Framework 4.8.1
It requires .NET Framework 4.8.1 or later version.
Lib\net6.0\EAGetMail.dll Built with .NET 6.0
It requires .NET 6.0 or later version.
Lib\net7.0\EAGetMail.dll Built with .NET 7.0
It requires .NET 7.0 or later version.
Lib\net8.0\EAGetMail.dll Built with .NET 8.0
It requires .NET 8.0 or later version.
Lib\netstandard2.0\EAGetMail.dll Built with .NET Standard 2.0
It requires .NET Standard 2.0 or later version.
Lib\net20-cf\EAGetMail.dll Built with .NET Compact Framework 2.0
It requires .NET Compact Framework 2.0, 3.5 or later version.
Lib\net35-cf\EAGetMail.dll Built with .NET Compact Framework 3.5
It requires .NET Compact Framework 3.5 or later version.

VB.NET - 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.


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

Imports System.Collections
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.IO
Imports EAGetMail 'imports EAGetMail namespace

Module Module1

    Private Function _formatHtmlTag(ByVal src As String) As String
        src = src.Replace(">", ">")
        src = src.Replace("<", "&lt;")
        Return src
    End Function

    Private Function _formatAddresses(ByVal addresses As MailAddress(), ByVal prefix As String) As String
        If addresses.Length = 0 Then
            Return ""
        End If

        Dim buffer As StringBuilder = New StringBuilder()
        buffer.Append(String.Format("<b>{0}:</b> ", prefix))

        For i As Integer = 0 To addresses.Length - 1

            If i < addresses.Length - 1 Then
                buffer.Append("; ")
            End If

        Return buffer.ToString()
    End Function

    ' 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 emlFile As String, ByVal htmlFile As String, ByVal attachmentFolder As String)
        Dim mail As Mail = New Mail("TryIt")
        mail.Load(emlFile, False)

        If mail.IsEncrypted Then
                ' This email is encrypted, we decrypt it by user default certificate.
                ' you can also use specified certificate like this
                ' cert = new Certificate()
                ' cert.Load("c:\test.pfx", "pfxpassword", Certificate.CertificateKeyLocation.CRYPT_USER_KEYSET)
                ' mail = mail.Decrypt(cert)
                mail = mail.Decrypt(Nothing)
            Catch ep As Exception
            End Try
        End If

        If mail.IsSigned Then
                ' This email is digital signed.
                Dim signerCertificate As Certificate = mail.VerifySignature()
                Console.WriteLine("This email contains a valid digital signature.")
            Catch ep As Exception
            End Try
        End If

        ' Decode winmail.dat (Outlook TNEF Stream) automatically.
        ' also convert RTF body to HTML body automatically

        Dim html As String = mail.HtmlBody
        Dim header As StringBuilder = New StringBuilder()

        header.Append("<font face=""Courier New,Arial"" size=2>")
        header.Append("<b>From:</b> " & _formatHtmlTag(mail.From.ToString()) & "<br>")

        header.Append(_formatAddresses(mail.[To], "To"))
        header.Append(_formatAddresses(mail.Cc, "Cc"))

        header.Append(String.Format("<b>Subject:</b>{0}<br>" & vbCrLf, _formatHtmlTag(mail.Subject)))

        Dim attachments As Attachment() = mail.Attachments

        If attachments.Length > 0 Then
            If Not Directory.Exists(attachmentFolder) Then
            End If

            header.Append("<b>Attachments:</b> ")
            For i As Integer = 0 To attachments.Length - 1
                Dim attachment As Attachment = attachments(i)
                Dim attachmentName As String = String.Format("{0}\{1}", attachmentFolder, attachment.Name)
                attachment.SaveAs(attachmentName, True)
                header.Append(String.Format("<a href=""{0}"" target=""_blank"">{1}</a> ", attachmentName, attachment.Name))

                If attachment.ContentID.Length > 0 Then
                    ' Show embedded image.
                    html = html.Replace("cid:" & attachment.ContentID, attachmentName)
                End If
        End If

        ' Change original meta header encoding to utf-8
        Dim reg As Regex = New Regex("(<meta[^>]*charset[ " & vbTab & "]*=[ " & vbTab & """]*)([^<> " & vbCrLf & """]*)", RegexOptions.Multiline Or RegexOptions.IgnoreCase)
        html = reg.Replace(html, "$1utf-8")

        If Not reg.IsMatch(html) Then
            header.Insert(0, "<meta HTTP-EQUIV=""Content-Type"" Content=""text/html; charset=utf-8"">")
        End If

        html = header.ToString() & "<hr>" & html

        Using stream As FileStream = New FileStream(htmlFile, FileMode.Create, FileAccess.Write, FileShare.None)
            Dim buffer As Byte() = Encoding.UTF8.GetBytes(html)
            stream.Write(buffer, 0, buffer.Length)
        End Using

    End Sub

    Sub ConvertMailToHtml(ByVal fileName As String)
            Dim pos As Integer = fileName.LastIndexOf(".")
            Dim attachmentFolder As String = fileName.Substring(0, pos)
            Dim htmlFile As String = fileName.Substring(0, pos) & ".htm"

            If Not File.Exists(htmlFile) Then
                ' We haven't generate the html for this email, generate it now.
                _GenerateHtmlForEmail(fileName, htmlFile, attachmentFolder)
            End If

            Console.WriteLine("Please open {0} to browse your email", htmlFile)
        Catch ep As Exception
        End Try
    End Sub
    Sub Main()
            ConvertMailToHtml("c:\my folder\test.eml")
        Catch ex As Exception
        End Try

    End Sub
End Module



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