Checking for New Mail

Checking for New Mail

The Inbox contains new messages. When users refer to new messages, they can mean messages that have arrived after the last time messages were read, or they can mean all unread messages. Depending on the needs of your application users, your applications can check various Message object properties to determine whether there is new mail.

You can force immediate delivery of any pending messages by calling the Session object’s DeliverNow method.

The following sample code tracks new messages by checking for messages in the Inbox with the Unread property value equal to True:

' Function: Util_CountUnread
' Purpose:  Count unread messages in a folder
'
Function Util_CountUnread()
Dim cUnread As Integer ' counter

    On Error GoTo error_olemsg
    If objMessages Is Nothing Then
        MsgBox "must select a Messages collection"
        Exit Function
    End If
    Set objMessage = objMessages.GetFirst
    cUnread = 0
    While Not objMessage Is Nothing ' loop through all messages
        If True = objMessage.Unread Then
            cUnread = cUnread + 1
        End If
        Set objMessage = objMessages.GetNext
    Wend
    MsgBox "Number of unread messages = " & cUnread
    Exit Function

error_olemsg:
    MsgBox "Error " & Str(Err) & ": " & Error$(Err)
    Resume Next

End Function
 

You can also check for new messages by counting the messages received after a specified time. For example, your application can maintain a variable that represents the time of the latest message received, based on the Message object’s TimeReceived property. The application can periodically check for all messages with a TimeReceived value greater than the saved value. When new messages are found, the application increments its count of new messages and updates the saved value.

With the CDO Library version 1.1 or later, you can use the Messages collection’s Filter property to obtain a MessageFilter object. Setting the message filter’s TimeFirst or Unread property reduces the number of messages presented to the loop doing the counting or other processing of new messages.

See Also

Filtering Messages in a Folder, Reading a Message from the Inbox

See Also

Concepts

Programming Tasks