Parse Email Body, Attachment and Convert Email to HTML page in VB.NET

In previous section, I introduced how to parse winmail.dat. In this section, I will introduce how to parse email body and attachment, then convert email to a HTML page and display it using Web browser in VB.NET.


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.


Remarks: All of examples in this section are based on first section: A simple VB.NET 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.NET Example - Convert email to HTML]

The following example codes demonstrate how to use EAGetMail POP3 component to convert email to HTML page.


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

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

In EAGetMail installer, there are many samples demonstrate how to use Web browser control to display the email, I suggest that you download it and have a try

pop3, imap4 samples

Next Section

At next section I will introduce how to parse Non-delivery report.



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