Search for new messages

The sample downloads new messages only, skipping already received messages, and displays "Subject:" for each new message.

Note: This sample provides only simple version of the code. More detailed documentation and code samples are available in Receiving new messages from POP3 server tutorial.

Basically, the POP3 protocol has no features to determine whether a message has already been downloaded. However, you can implement this with Unique-ID. Unique-ID is a unique identifier of an e-mail on the POP3 server.

Search method of POP3 object returns an array which contains Unique-ID's of all messages in the mailbox. First element of the array contains Unique-ID of the first message in the mailbox, second element - Unique-ID of the second message, and so on.

The idea is as follows...

Firstly, your application must store Unique-ID's of already received messages in a local database.

Next time before you start downloading e-mail, retrieve Unique-ID's for all messages in the mailbox (using Search method) and look up them in the database. If Unique-ID of some message is found in the database, the message is old; otherwise, it is new. Thus you can filter out Unique-ID of every old message.

In real code, sample function IsNewMessage() should query the database for the given Unique-ID value and return True if the Unique-ID was NOT found in the database. In this sample, no real database is used and IsNewMessage() always returns True.

Last thing is to download new messages knowing their Unique-ID's. Since RetrieveSingleMessage method expects message numbers but not Unique-ID's, it is required to get message number from Unique-ID. It's simple: an index of the Unique-ID value in the Unique-ID array is the message number.

Visual Basic

' This is main function. It retrieves new messages from
' the POP3 server and shows "Subject:" field of each
' retrieved message.
Sub RetrieveNewMessages()
  Dim objPOP3, objMsg, I

  ' Array of Unique-IDs (taken from the POP3 server)
  Dim arrIDs

  ' Create POP3 object
  Set objPOP3 = CreateObject("MailBee.POP3")

  ' Unlock POP3 object
  objPOP3.LicenseKey = "put your license key here"

  ' Set POP3 server name
  objPOP3.ServerName = "mail.server.com"

  ' Set user credentials
  objPOP3.UserName = "username"
  objPOP3.Password = "password"

  ' Connect to the server and log in the mailbox
  If objPOP3.Connect Then

    ' Get Unique-IDs for all messages in the mailbox
    arrIDs = objPOP3.Search

    ' Iterate through all messages in the mailbox
    For I = 1 To objPOP3.MessageCount

      ' Search Unique-ID of the message in the database
      If IsNewMessage(arrIDs(I)) Then
      
        ' Download new message entirely
        Set objMsg = objPOP3.RetrieveSingleMessage(I)
        
        ' Display the message's Subject
        MsgBox objMsg.Subject
      End If
    Next

    ' Close the connection
    objPOP3.Disconnect
  Else
    ' Display error information
    MsgBox "Error #" & objPOP3.ErrCode
    MsgBox "Server response: " & objPOP3.ServerResponse
  End If
End Sub

' This helper function should lookup the ID in the
' database, and then return True if the ID does not
' exist in the list of already downloaded messages.
Function IsNewMessage(ID)
  ' For demo purposes, the function always returns True.
  IsNewMessage = True
End Function

ASP

<%
' This is main function. It retrieves new messages from
' the POP3 server and shows "Subject:" field of each
' retrieved message.
Sub RetrieveNewMessages()
  Dim objPOP3, objMsg, I

' Array of Unique-IDs (taken from the POP3 server)
  Dim arrIDs As Variant

  ' Create POP3 object
  Set objPOP3 = Server.CreateObject("MailBee.POP3")

  ' Unlock POP3 object
  objPOP3.LicenseKey = "put your license key here"

  ' Set POP3 server name
  objPOP3.ServerName = "mail.server.com"

  ' Set user credentials
  objPOP3.UserName = "username"
  objPOP3.Password = "password"

  ' Connect to the server and log in the mailbox
  If objPOP3.Connect Then

    ' Get Unique-IDs for all messages in the mailbox
    arrIDs = objPOP3.Search

    ' Iterate through all messages in the mailbox
    For I = 1 To objPOP3.MessageCount

      ' Search Unique-ID of the message in the database
      If IsNewMessage(arrIDs(I)) Then
      
        ' Download entire message
        Set objMsg = objPOP3.RetrieveSingleMessage(I)
        
        ' Display the message's Subject
        Response.Write objMsg.Subject & "<br>"
      End If
    Next

    ' Close the connection
    objPOP3.Disconnect
  Else
    ' Display error information
    Response.Write "Error #" & objPOP3.ErrCode & "<br>"
    Response.Write "Server response: " & objPOP3.ServerResponse
  End If
End Sub

' This helper function should lookup the ID in the
' database, and then return True if the ID does not
' exist in the list of already downloaded messages.
Function IsNewMessage(ID)
  ' For demo purposes, the function always returns True.
  IsNewMessage = True
End Function
%>

See Also:
"Receiving new messages from POP3 server" Tutorial
Search Method