Using UIDL Function to Mark the Email has been downloaded/read in VB.NET

In previous section, I introduced how to use event handler. In this section, I will introduce how to use UIDL function to mark the email has been downloaded in VB.NET.

Introduction

If you want to leave a copy of email on the server, you should not call Delete method. However, there is a problem, how can you know if the email has already been downloaded? If there is a way to identify the downloaded email, you can avoid downloading the duplicated email from your POP3/IMAP4 server.

Note

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 - IMAP4 Solution]

Every email has a unique identifier (UIDL) on IMAP4 server. It is a 32bit integer and it is always unique in your email account life time. So we can use the integer as file name to identify if the email has been downloaded. In order to run it correctly, please change email server, user, password, folder, file name values.

Imports System.IO
Imports EAGetMail 'imports EAGetMail namespace

Module Module1
    Sub Main()
        ' Create a folder named "inbox" under current directory
        ' to save the email retrieved.
        Dim curpath As String = Directory.GetCurrentDirectory()
        Dim mailbox As String = [String].Format("{0}\inbox", curpath)

        ' If the folder is not existed, create it.
        If Not Directory.Exists(mailbox) Then
            Directory.CreateDirectory(mailbox)
        End If

        Dim oServer As New MailServer("imap4.emailarchitect.net", _
            "test@emailarchitect.net", "testpassword", ServerProtocol.Imap4)
        Dim oClient As New MailClient("TryIt")

        ' Set IMAP4 server port
        oServer.Port = 143

        ' If your IMAP4 server requires SSL connection,
        ' Please add the following codes:
        ' oServer.SSLConnection = True
        ' oServer.Port = 993

        Try
            oClient.Connect(oServer)
            Dim infos As MailInfo() = oClient.GetMailInfos()
            For i As Integer = 0 To infos.Length - 1
                Dim info As MailInfo = infos(i)
                Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}", _
                        info.Index, info.Size, info.UIDL)

                ' Using IMAP UIDL as the file name.
                Dim fileName As String = String.Format("{0}\{1}.eml", _
                        mailbox, info.UIDL )

                If File.Exists( fileName ) Then

                ' This email has been downloaded,  do not download it again.
                    Continue For
                End If

                ' Receive email from IMAP4 server
                Dim oMail As Mail = oClient.GetMail(info)

                Console.WriteLine("From: {0}", oMail.From.ToString())
                Console.WriteLine("Subject: {0}" & vbCr & vbLf, oMail.Subject)

                ' Save email to local disk
                oMail.SaveAs(fileName, True)

                ' Do not delete email from IMAP4 server.
            Next

            ' Quit
            oClient.Quit()
        Catch ep As Exception
            Console.WriteLine(ep.Message)
        End Try

    End Sub
End Module

POP3/IMAP4/Exchange Web Service/WebDAV Solution

There is a little bit different in POP3 server. The UIDL is only unique in the email life time. That means if the email was deleted from the server, other email can use the old unique identifier. Another problem is: UIDL in POP3 server can be any number or characters, so we cannot use UIDL as the file name, because UIDL may contain invalid characters for file name.

UIDL is also unique in Exchange Web Service/WebDAV, but the UIDL is string but not integer. UIDL in Exchange server may contain invalid characters for file name, so we cannot use UIDL as the file name either.

To solve this problem, we have to store the UIDL to a txt file and synchronize it with server every time.

Please have a look at the following example code. It works with both POP3/IMAP4/Exchange Web Service/WebDAV protocol.

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

Module Module1
    Public Class MyMailClient
        Private m_arUidl As ArrayList = New ArrayList
        Private m_uidlfile As String = "uidl.txt"
        Private m_curpath As String = ""
#Region "UIDL Functions"
        ' Uidl is the identifier of every email on POP3/IMAP4 server, to avoid
        ' retrieve the same email from server more than once, we record the
        ' email uidl retrieved every time if you delete the email from server
        ' every time and not to leave a copy of email on the server, then please
        ' remove all the function about uidl.
        Private Function _FindUIDL(ByVal infos() As MailInfo, ByVal uidl As String) As Boolean
            Dim count As Integer = infos.Length
            For i As Integer = 0 To count - 1
                If String.Compare(infos(i).UIDL, uidl, False) = 0 Then
                    _FindUIDL = True
                    Exit Function
                End If
            Next
            _FindUIDL = False
        End Function

        ' Remove the local uidl which is not existed on the server.
        Private Sub _SyncUIDL(ByVal oServer As MailServer, ByVal infos() As MailInfo)
            Dim s As String = String.Format("{0}#{1} ", oServer.Server, oServer.User)
            Dim bcontinue As Boolean = False
            Dim n As Integer = 0

            Do
                bcontinue = False
                Dim count As Integer = m_arUidl.Count
                For i As Integer = n To count - 1
                    Dim x As String = m_arUidl(i)
                    If String.Compare(s, 0, x, 0, s.Length, True) = 0 Then

                        Dim pos As Integer = x.LastIndexOf(" ")
                        If pos <> -1 Then
                            Dim uidl As String = x.Substring(pos + 1)
                            If (Not _FindUIDL(infos, uidl)) Then

                                ' this uidl doesn't exist on server,
                                ' we should remove it from local uidl list.
                                bcontinue = True
                                n = i
                                m_arUidl.RemoveAt(i)
                                Exit For
                            End If
                        End If
                    End If
                Next
            Loop While (bcontinue)

        End Sub

        Private Function _FindExistedUIDL(ByVal oServer As MailServer, _
                ByVal uidl As String) As Boolean
            Dim s As String = String.Format("{0}#{1} {2}", _
                oServer.Server.ToLower(), oServer.User.ToLower(), uidl)
            Dim count As Integer = m_arUidl.Count
            For i As Integer = 0 To count - 1
                Dim x As String = m_arUidl(i)
                If String.Compare(s, x, False) = 0 Then
                    _FindExistedUIDL = True
                    Exit Function
                End If
            Next
            _FindExistedUIDL = False
        End Function

        Private Sub _AddUIDL(ByVal oServer As MailServer, ByVal uidl As String)
            Dim s As String = String.Format("{0}#{1} {2}", _
                oServer.Server.ToLower(), oServer.User.ToLower(), uidl)
            m_arUidl.Add(s)
        End Sub

        Private Sub _UpdateUIDL()
            Dim s As New StringBuilder
            Dim count As Integer = m_arUidl.Count
            For i As Integer = 0 To count - 1
                s.Append(m_arUidl(i))
                s.Append(vbCrLf)
            Next

            Dim file As String = String.Format("{0}\{1}", m_curpath, m_uidlfile)
            Dim fs As FileStream = Nothing

            Try
                fs = New FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None)
                Dim data() As Byte = System.Text.Encoding.Default.GetBytes(s.ToString())
                fs.Write(data, 0, data.Length)
                fs.Close()
            Catch ep As Exception
                If Not (fs Is Nothing) Then
                    fs.Close()
                End If
                Throw ep
            End Try
        End Sub

        Private Sub _LoadUIDL()

            m_arUidl.Clear()
            Dim filename As String = String.Format("{0}\{1}", m_curpath, m_uidlfile)
            Dim read As StreamReader = Nothing

            Try
                read = File.OpenText(filename)
                Do While (True)
                    Dim line As String = read.ReadLine().Trim(vbCrLf & " " & vbTab.ToCharArray())
                    m_arUidl.Add(line)
                Loop
            Catch ep As Exception
            End Try

            If Not (read Is Nothing) Then
                read.Close()
            End If
        End Sub
#End Region

        Public Sub ReceiveEmail(ByVal oServer As MailServer, ByVal bLeaveCopy As Boolean)
            Dim oClient As New MailClient("TryIt")
            m_curpath = Directory.GetCurrentDirectory()
            Try
                ' Uidl is the identifier of every email on POP3/IMAP4 server, to
                ' avoid retrieve the same email from server more than once,
                ' we record the email uidl retrieved every time if you delete the
                ' email from server every time and not to leave a copy of email on
                ' the server, then please remove all the function about uidl.
                _LoadUIDL()

                Dim mailFolder As String = [String].Format("{0}\inbox", m_curpath)
                If Not Directory.Exists(mailFolder) Then
                    Directory.CreateDirectory(mailFolder)
                End If

                Console.WriteLine("Connecting server ... ")
                oClient.Connect(oServer)
                Dim infos As MailInfo() = oClient.GetMailInfos()
                Console.WriteLine("Total {0} email(s)", infos.Length)

                _SyncUIDL(oServer, infos)
                Dim count As Integer = infos.Length

                For i As Integer = 0 To count - 1
                    Dim info As MailInfo = infos(i)
                    If _FindExistedUIDL(oServer, info.UIDL) Then
                        ' this email has existed on local disk.
                        Continue For
                    End If

                    Console.WriteLine("Retrieving {0}/{1}...", info.Index, count)

                    Dim oMail As Mail = oClient.GetMail(info)
                    Dim d As System.DateTime = System.DateTime.Now
                    Dim cur As New System.Globalization.CultureInfo("en-US")
                    Dim sdate As String = d.ToString("yyyyMMddHHmmss", cur)
                    Dim fileName As String = [String].Format("{0}\{1}{2}{3}.eml", _
                        mailFolder, sdate, d.Millisecond.ToString("d3"), i)
                    oMail.SaveAs(fileName, True)

                    If bLeaveCopy Then

                        ' Add the email uidl to uidl file to avoid we retrieve it next time.
                        _AddUIDL(oServer, info.UIDL)
                    End If
                Next

                If Not bLeaveCopy Then
                    Console.WriteLine("Deleting ...")
                    For i As Integer = 0 To count - 1
                        oClient.Delete(infos(i))
                    Next
                End If

                Console.WriteLine("Completed")
                oClient.Quit()
            Catch ep As Exception
                Console.WriteLine(ep.Message)
            End Try

            ' Update the uidl list to a text file and then we can load it next time.
            _UpdateUIDL()
        End Sub
    End Class

    Sub Main()
        ' Create a folder named "inbox" under current directory
        ' to save the email retrieved.
        Dim curpath As String = Directory.GetCurrentDirectory()
        Dim mailbox As String = [String].Format("{0}\inbox", curpath)

        ' If the folder is not existed, create it.
        If Not Directory.Exists(mailbox) Then
            Directory.CreateDirectory(mailbox)
        End If

        Dim oServer As New MailServer("pop3.emailarchitect.net", _
            "test@emailarchitect.net", "testpassword", ServerProtocol.Pop3)
        Dim oClient As New MailClient("TryIt")

        ' If your POP3 server requires SSL connection,
        ' Please add the following codes:
        ' oServer.SSLConnection = True
        ' oServer.Port = 995

        Dim MyClient As New MyMailClient
        MyClient.ReceiveEmail(oServer, True)

    End Sub
End Module

UIDLManager Object

With EAGetMail 4.0, it provides a new class named “UIDLManager”. This object provides an easy way to maintain UIDL between your server and your local client. How does it work? It stores UIDL collection to a local disk file and you can use this object to add, remove and search UIDL with this local file. Then you don’t have to handle UIDL in your code. Please click here to learn more detail.

Mark Email as Read on IMAP4/Exchange Server

With IMAP4/Exchange Web Service/WebDAV protocol, you can also mark the email as read on the server, but POP3 doesn’t support this feature. Please refer to MarkAsRead method to learn more detail.

Next Section

At next section I will introduce how to download email in background.

Appendix

Comments

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