ANPOP Developers Center > Parse Email in Visual Basic.NET

Add Reference of ANPOP in your project

First of all, make sure ANPOP has been installed on your machine. Then create a Windows Form project in Visual Studio.NET:  select menu "Project" -> "Add Reference" -> COM, and select "ANPOP POP3 COMPONENT BUILD".

Declare and define objects

Imports ANPOPLib;
....
private m_oPop3 As New POPMAINClass();
private m_oMsg  As New POPMSGClass();	
private m_oStore As New MSGSTOREClass();

Although you can create new object instance each time, we recommend to declare object as member variable so that you just need to create it once and then enjoy  reuse those member variables without re-create it again.

Here I don't want to introduce how to receive email from pop3 server, you can refer to many samples in anpop installation package. In the following secion, I'll focus how to parse email and display it in WebBrowser control.

First of all, I use POPMSG object to parse email and generate a html file. And I save all attachments to a temporary directory. Then I use WebBrowser control to display this email.

Sub ShowEmail(ByVal messageId As String)
    Dim oStore As MSGSTOREClass = m_oLocalStore
    Dim oMsg As POPMSGClass = m_oLocalMsg
    Dim fileName, mainName, htmlName As String
    Dim pos As Long

    fileName = oStore.GetEmailFileName(SZ_INBOX, messageId)
    pos = fileName.LastIndexOf("\")
    fileName = fileName.Substring(pos + 1)
    pos = fileName.LastIndexOf(".")
    mainName = fileName.Substring(0, pos)
    htmlName = mainName & ".htm"
    htmlName = m_szFolder & "\" & htmlName

    Dim tempFolder As String 
    'temporary folder for saving attachment of current email.
    tempFolder = m_szFolder & "\" & mainName
    If oMsg.CreateFolder(tempFolder) <> 0 Then
        MessageBox.Show("Create temporary folder for attachments failed")
        Exit Sub
    End If
    'cached html file doesn't exist, then generate one.
    If Not System.IO.File.Exists(htmlName) Then 
        Dim bodyText, htmlText, summaryText As String
        oMsg.RawContent = oStore.GetEmailById(SZ_INBOX, messageId)
        bodyText = GetBody(oMsg)
        summaryText = GetSummary(oMsg)
        summaryText = summaryText & GetAttachments(oMsg, tempFolder, bodyText)&"<hr>"
        htmlText = summaryText & bodyText
        Dim cvt As System.Text.Encoding = System.Text.Encoding.Default
        Dim fStream As FileStream = File.Create(htmlName)
        Dim pData() As Byte = cvt.GetBytes(htmlText)
        fStream.Write(pData, 0, pData.Length)
        fStream.Close()
    End If

    webMail.Navigate(htmlName) 'show cached html file for current email.
End Sub

All emails are only parsed once, so you needn't to parse it each time.

A Total Sample

This sample demonstrates an email client, click here to download it.

Free Email Support

Not enough? Please contact our technical support team.

Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered User)

Remarks
We usually reply emails in 24hours. The reason for getting no response is likely that your smtp server bounced our reply. In this case, please try to use another email address to contact us. Your Hotmail or Yahoo email account is recommended.



2001-2010 © Copyright AdminSystem Software Limited. All rights reserved.   About us  Site Map