Downloading the list of messages

This sample connects to IMAP4 server, logs in e-mail account, selects "Inbox" folder, downloads headers for all e-mails in the folder. For each e-mail, "From:" field and number of attachments are displayed.

The IMAP4 protocol allows to examine messages in greater detail than the POP3 protocol. With IMAP4, you can get message envelopes, body structures and normal POP3-style message headers.

Message envelope (available through Envelope object) is similar to message headers but provides some properties which message headers don't (such as datetime of the moment when the e-mail was received by IMAP4 server). On other hand, envelope contains only basic headers such as "Subject:", "To:", "From:", "Reply-To:", "Message-ID:", "Date:" and so on. To get custom headers (such as "Organization" or "X-Priority") you still need to retrieve message headers.

If custom headers are needed, you can tell MailBee to also retrieve message headers when retrieving envelopes. Use RetrieveEnvelopesEx method with AlsoGetMessagePreview parameter set to True.

Body structure allows you to learn message structure (number and names of attachments, types of message bodies, and so on). You can get body structures together with envelopes using RetrieveEnvelopesEx method with AlsoGetBodyStructure parameter set to True.

Body structure is organized in the tree of BodyPartStructure objects. E-mail messages often consist of several parts which can contain another parts (and these parts can also contain smaller parts). Usually, nesting level does not exceed the number of 3, but can potentially be unlimited. BodyPartStructure objects provide body structures of contained smaller parts (if any) in SubParts property. Each object in SubParts collection is also a BodyPartStructure object. To scan the whole body structure (e.g. to count all attachments in the e-mail) it's required to scan all BodyPartStructure objects with all their subparts.

Visual Basic

' This function performs recursively through
' all body parts of the e-mail, counting the number
' of the attachments
Function GetAttachmentsCount(objBodyPart)
  Dim objSubPart

  GetAttachmentsCount = 0
  If objBodyPart.IsMultipart Then
    ' Body part is grouping part, dig into subparts
    For Each objSubPart In objBodyPart.SubParts
      GetAttachmentsCount = GetAttachmentsCount + GetAttachmentsCount(objSubPart)
    Next
  ElseIf objBodyPart.Disposition = "attachment" Then
    ' Body part is attachment
    GetAttachmentsCount = 1
  End If
End Function

Dim objIMAP4, objEnvelopes, objEnvelope

Set objIMAP4 = CreateObject("MailBee.IMAP4")

' 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

    ' Download envelopes together with body structures for
    ' all e-mails in the folder
    Set objEnvelopes = _
      objIMAP4.RetrieveEnvelopesEx(1, objIMAP4.MessageCount, False, True)

    If Not objIMAP4.IsError Then
      For Each objEnvelope In objEnvelopes
        ' Display "Subject:" (taken from envelope) and number of
        ' attachments (taken from bdoy structure) for every e-mail
        MsgBox "Subject: " & objEnvelope.Subject & vbCrLf & _
          "Number of attachments: " & _
          GetAttachmentsCount(objEnvelope.BodyStructure)
      Next
    Else
      ' Display error information
      MsgBox "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
    End If
  Else
    ' Display error information
    MsgBox "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
  End If

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

ASP

<%
' This function performs recursively through
' all body parts of the e-mail, counting the number
' of the attachments
Function GetAttachmentsCount(objBodyPart)
  Dim objSubPart

  GetAttachmentsCount = 0
  If objBodyPart.IsMultipart Then
    ' Body part is grouping part, dig into subparts
    For Each objSubPart In objBodyPart.SubParts
      GetAttachmentsCount = GetAttachmentsCount + GetAttachmentsCount(objSubPart)
    Next
  ElseIf objBodyPart.Disposition = "attachment" Then
    ' Body part is attachment
    GetAttachmentsCount = 1
  End If
End Function

Dim objIMAP4, objEnvelopes, objEnvelope

Set objIMAP4 = Server.CreateObject("MailBee.IMAP4")

' 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

    ' Download envelopes together with body structures for
    ' all e-mails in the folder
    Set objEnvelopes = _
      objIMAP4.RetrieveEnvelopesEx(1, objIMAP4.MessageCount, False, True)

    If Not objIMAP4.IsError Then
      For Each objEnvelope In objEnvelopes
        ' Display "Subject:" (taken from envelope) and number of
        ' attachments (taken from bdoy structure) for every e-mail
        Response.Write "Subject: " & objEnvelope.Subject & "<br>" & _
          "Number of attachments: " & _
          GetAttachmentsCount(objEnvelope.BodyStructure) & "<br><br>"
      Next
    Else
      ' Display error information
      Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
    End If
  Else
    ' Display error information
    Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
  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:

RetrieveEnvelopes Method

RetrieveEnvelopesEx Method