IMAP4 Object

You can use the IMAP4 object to establish the connection with the IMAP4 server, select and manage the IMAP4 mailboxes, examine and alter properties of the messages, search for messages that match a criteria, and download entire messages.

Another features of the IMAP4 object include secure authentication options, ability to execute user-supplied commands, upload messages to the server and copy messages between mailboxes, and more.

Note: By default, the IMAP4 object neither processes Windows events nor fires its own events. This ensures maximum performance in the server environment such as ASP, but causes the user interface of desktop applications to be not responsive. To enable event handling and firing, you can set EnableEvents property value to True.

Syntax

IMAP4.property|method

 

Properties
AuthMethod Specifies the authentication method to use when logging in the IMAP4 mail account.
Busy Indicates whether the IMAP4 object is in the progress of sending or receiving data.
ClientRequest Returns the last command string sent to the IMAP4 server.
Codepage Specifies the codepage into which to convert retrieved messages and envelopes.
CodepageMode Specifies whether to use Codepage property if target codepage can be auto-detected.
Connected Indicates whether the IMAP4 session is active.
EnableEvents Specifies whether the IMAP4 object will handle and fire events.
EnableLogging Specifies whether the IMAP4 object must log the IMAP4 session into a file.
ErrCode Completion status of the last command.
ErrDesc Contains textual error description if the last command has completed with an error.
IsError Indicates whether the last command has completed with errors.
Licensed Indicates whether valid license key has been assigned to the IMAP4 object.
LicenseKey Sets the license key to be used with the IMAP4 object.
LogFilePath Sets filename of the IMAP4 session log file.
MessageCount The total number of messages in the currently selected mailbox.
Password The password of the user account on the IMAP4 server.
PortNumber The port number of the IMAP4 service on the server. Defaults to 143.
RecentCount The number of recent messages in the currently selected mailbox.
Selected Indicates whether a mailbox has been selected.
SendDelay Gets or sets the delay in milliseconds between two successive requests to the IMAP server.
ServerName The IMAP4 server name.
ServerResponse Returns the string that contains the entire last reply from the IMAP4 server.
ServerStatusResponse The string containing completion status part of the last reply from the IMAP4 server.
SmartParsing

Enables better error recovery from bad-formatted responses from some IMAP servers.

SSL Specifies the SSL object to be used to establish secure TLS connection with the IMAP4 server.
Timeout The timeout value in seconds.
UIDValidity Returns UIDVALIDITY value of the currently selected IMAP4 mailbox.
UserName The name of the user account on the IMAP4 server.
UTF7EncodeMailboxNames Tells MailBee whether to automatically encode IMAP mailbox names to UTF-7M when sending requests to the server.
Methods
Abort Aborts current operation and immediately closes the IMAP4 session.
AppendMessage Uploads a message to the specified mailbox.
ClearLog Clears the IMAP4 session log file.
Connect Connects to the IMAP4 server and logs in the user mail account.
CopyMessages Copies specified messages from the currently selected mailbox to another mailbox.
CreateMailbox Creates a mailbox in the user account on the IMAP4 server.
DeleteMailbox Deletes specified mailbox from the user account.
DeleteMessages Marks the specified message in the currently selected mailbox as "Deleted".
Disconnect Disconnects from the IMAP4 server.
ExamineMailbox Selects specified mailbox on the IMAP4 server for read-only access.
Expunge Permanently removes all messages marked as "Deleted" from the currently selected mailbox.
ExpungeAndClose Permanently removes all messages marked as "Deleted" and closes currently selected mailbox.
FromUTF7 Decodes the specified UTF-7M string into regular string.
GetMailboxQuota Retrieves the quota size for the mailbox or the entire mail account in kilobytes.
Idle Receives status notifications from the server in IDLE mode.
Ping Sends NOOP command to the IMAP4 server.
RenameMailbox Renames specified mailbox on the IMAP4 server.
RetrieveEnvelopes Retrieves one or more message envelopes from the currently selected mailbox.
RetrieveEnvelopesEx Retrieves message envelopes with extended information from the currently selected mailbox.
RetrieveMailboxes Retrieves a list of mailboxes available in the user account on the IMAP4 server.
RetrieveMailboxesEx Retrieves Mailboxes collection which contains mailboxes of the IMAP4 account
RetrieveMessagesSize Retrieves total size in bytes of one or more messages in the currently selected mailbox.
RetrieveSingleMessage Downloads entire message from the currently selected IMAP4 mailbox.
RetrieveSingleMessageHeaders Downloads headers for the message from the currently selected IMAP4 mailbox.
RetrieveTotalSize Retrieves total size in bytes of all messages in the currently selected mailbox.
Search Searches the currently selected mailbox for messages that conform to specified criteria.
SelectMailbox Selects specified mailbox on the IMAP4 server.
SendCommand Executes user-supplied command on the IMAP4 server.
SetFlagsForMessage Sets the flags for specified message in the currently selected mailbox.
Subscribe Subscribes specified mailbox.
ToUTF7 Encodes the specified string into UTF-7M.
Unsubscribe Unsubscribes specified mailbox.
Events
OnAlert Fires when there is an [ALERT] message in the IMAP4 server response.
OnIdle Fires when another 10 millisecond period elapses during idling.
OnReceiveData Fires when the IMAP4 object receives new chunk of data from the IMAP server.
OnStatusChange Fires when mailbox status changes: new message has arrived, a message has been deleted, number of messages changes, and so on.

 

Remarks

During the development, it's recommended to enable logging the IMAP4 session into a file by setting EnableLogging and LogFilePath properties. The log file allows you to discover typical problems quickly and easily.

You can find more information on typical problems resolution in Troubleshooting section of the IMAP4 samples.

 

Example

The following example retrieves new messages from the user account on the IMAP4 server, and displays each retrieved message. The sample is presented separately in Visual Basic version and ASP version since they differ quite enough.

Note: For simplicity, the example neither handles errors nor logs the IMAP4 session into a file. Search for new messages sample provides the code for error-checking and logging the IMAP4 session.

[Visual Basic]
Dim objIMAP4, objMsg, arrNew, I

' Create IMAP4 object
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
  objIMAP4.SelectMailbox "Inbox"

  ' Get Unique-IDs of new messages
  arrNew = objIMAP4.Search(True, "new")
    
  ' 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)

    ' Display the message
    MsgBox _
      "From: " & objMsg.FromAddr & vbCrLf & _
      "To: " & objMsg.ToAddr & vbCrLf & _
      "Subject: " & objMsg.Subject & vbCrLf & _
      "Date: " & objMsg.GetDateFromString(objMsg.Date) & vbCrLf & vbCrLf & _
      objMsg.BodyText
  Next

  ' Close the connection
  objIMAP4.Disconnect
End If

 

[ASP]
<%
Dim objIMAP4, objMsg, arrNew, I

' Create IMAP4 object
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
  objIMAP4.SelectMailbox "Inbox"

  ' Get Unique-IDs of new messages
  arrNew = objIMAP4.Search(True, "new")
    
  ' 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)

    ' Display the message.
    ' We use Server.HTMLEncode to convert '<' and '>'
    ' characters which can disturb the output if they
    ' are contained in the message headers.
    Response.Write _
      "From: " & Server.HTMLEncode(objMsg.FromAddr) & "<br>" & _
      "To: " & Server.HTMLEncode(objMsg.ToAddr) & "<br>" & _
      "Subject: " & Server.HTMLEncode(objMsg.Subject) & "<br>" & _
      "Date: " & objMsg.GetDateFromString(objMsg.Date) & "<br><br>" & _
      objMsg.BodyText
  Next

  ' Close the connection
  objIMAP4.Disconnect
End If
%>

 

See Also

Envelope Object, Message Object, RetrieveEnvelopes Method

 


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