Search for unread messages

Visual Basic Code | ASP Code

This sample connects to IMAP4 server, logs in e-mail account, selects "Inbox" folder and downloads unread (UNSEEN in IMAP) messages.

Unlike POP3, IMAP4 protocol remembers which messages have been already downloaded. This makes it easy to determine which messages are new.

Note: If e-mail account is accessed by multiple mail agents, IMAP4's unread messages detection won't work: messages not yet received by one mail agent can be already received (and marked as read on the server) by another mail agent. In other words, when the same message can be unread for one mail agent and read for another, your app should not rely on IMAP4 read/unread messages detection. In this case, you can use the same approach as for POP3 protocol (see Search for new messages POP3 sample).

[Visual Basic]:

Dim objIMAP4, objMsg, arrNew, I

' Create IMAP4 object
Set objIMAP4 = CreateObject("MailBee.IMAP4")

' Enable logging IMAP4 session into a file
objIMAP4.EnableLogging = True
objIMAP4.LogFilePath = "C:\Temp\imap4_log.txt"

' Unlock IMAP4 object
objIMAP4.LicenseKey = "put your license key here"

' Set IMAP4 server name
objIMAP4.ServerName = "mail.server.com"

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

' Connect to the server and
' log in email account
If objIMAP4.Connect Then

  ' Select Inbox folder
  If objIMAP4.SelectMailbox("Inbox") Then

    ' Get Unique-IDs of unread messages
    arrNew = objIMAP4.Search(True, "unseen")
    
    If Not objIMAP4.IsError Then

      ' Loop through new messages
      For I = LBound(arrNew) To UBound(arrNew)

        ' Completely retrieve message by its Unique-ID
        Set objMsg = objIMAP4.RetrieveSingleMessage(arrNew(I), True)

        If Not objIMAP4.IsError Then
          ' Display message body
          MsgBox objMsg.BodyText
        Else
          ' Display error information
          MsgBox "Error #" & objIMAP4.ErrCode & ", " & _
            objIMAP4.ErrDesc
        End If
      Next
    Else
      ' Display error information
      MsgBox "Error #" & objIMAP4.ErrCode & ", " & _
        objIMAP4.ErrDesc
    End If
  Else
    ' Display error information
    MsgBox "Error #" & objIMAP4.ErrCode
    MsgBox "Server response: " & objIMAP4.ServerResponse
  End If

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

[ASP]:

<%
Dim objIMAP4, objMsg, arrNew, I

' Create IMAP4 object
Set objIMAP4 = Server.CreateObject("MailBee.IMAP4")

' Enable logging IMAP4 session into a file
objIMAP4.EnableLogging = True
objIMAP4.LogFilePath = "C:\Temp\imap4_log.txt"

' Unlock IMAP4 object
objIMAP4.LicenseKey = "put your license key here"

' Set IMAP4 server name
objIMAP4.ServerName = "mail.server.com"

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

' Connect to the server and
' log in email account
If objIMAP4.Connect Then

  ' Select Inbox folder
  If objIMAP4.SelectMailbox("Inbox") Then

    ' Get Unique-IDs of unread messages
    arrNew = objIMAP4.Search(True, "unseen")
    
    If Not objIMAP4.IsError Then

      ' Loop through new messages
      For I = LBound(arrNew) To UBound(arrNew)

        ' Completely retrieve a message by its Unique-ID
        Set objMsg = objIMAP4.RetrieveSingleMessage(arrNew(I), True)

        If Not objIMAP4.IsError Then
          ' Display message body
          Response.Write objMsg.BodyText & "<br>"
        Else
          ' Display error information
          Response.Write "Error #" & objIMAP4.ErrCode & ", " & _
            objIMAP4.ErrDesc & "<br>"
        End If
      Next
    Else
      ' Display error information
      Response.Write "Error #" & objIMAP4.ErrCode & ", " & _
        objIMAP4.ErrDesc
    End If
  Else
    ' Display error information
    Response.Write "Error #" & objIMAP4.ErrCode & "<br>"
    Response.Write "Server response: " & objIMAP4.ServerResponse
  End If

  ' Close the connection
  objIMAP4.Disconnect
Else
  ' Display error information
  Response.Write "Error #" & objIMAP4.ErrCode & "<br>"
  Response.Write "Server response: " & objIMAP4.ServerResponse
End If
%>
See Also:
Search Method

 


Send feedback to AfterLogic
Copyright © 2002-2024, AfterLogic Corporation. All rights reserved.