The IMAP protocol provides extensive support for message management. The Imap component implements most IMAP message-based functionality through the use of two objects. The ImapMessage object represents a message on the IMAP server. Use this object to modify message properties, such as setting flags. The ImapMessageCollection object represents a collection of ImapMessage objects. Use this object to perform bulk operations on messages. The following examples illustrate how messages can be manipulated through the use of these objects.
Log into the server
No messages can be manipulated without first logging into the server.
| C# | Copy Code |
|---|---|
// imap1.Login("myserver", "username", "password"); | |
| Visual Basic | Copy Code |
|---|---|
' Imap1.Login("myserver", "username", "password") | |
At this point we are logged in and ready to access the mailboxes. Since the Imap.AutoList property is true, the component automatically issued a list, the results of which were used to populate the Imap.Mailboxes collection.
Access the messages.
The messages contained within any given mailbox are contained in an ImapMessageCollection accessible via the Messages property of the Mailbox object. NOTE: This collection is NOT populated until the ImapMessageCollection is accessed for the first time.
As stated above, since Imap.AutoList is true, the Imap component has already performed a list. The Imap component has also "SELECTED" a mailbox named "INBOX" (provided one exists). Imap.CurrentMailbox reflects this, and should contain a Mailbox object representing the mailbox "INBOX". Access this collection will cause a listing to be sent to the server, and populate the collection.
| C# | Copy Code |
|---|---|
// Display the message count Debug.WriteLine(imap1.CurrentMailbox.Messages.Count + " messages in INBOX"); | |
| Visual Basic | Copy Code |
|---|---|
' Display the message count Debug.WriteLine(Imap1.CurrentMailbox.Messages.Count + " messages in INBOX") | |
APPEND a new message to the INBOX
Use the ImapMessageCollection.Add method of the ImapMessageCollection to add a new message to the mailbox by using the APPEND command. For this example, we will just create a test message dynamically using the MessageStream object.
| C# | Copy Code |
|---|---|
// Create a new MessageStream object MessageStream msg = new MessageStream(); msg.To.Add(new MailAddress("you@test.com")); msg.From = new MailAddress("me@test.com"); msg.Subject = "test message"; msg.Text = "Hello!"; // Append this message to INBOX. This returns the ImapMessage object representing the new message. ImapMessage iMsg = imap1.CurrentMailbox.Messages.Add(msg); | |
| Visual Basic | Copy Code |
|---|---|
' Create a new MessageStream object Dim Msg As New MessageStream() Msg.To.Add(New MailAddress("you@test.com")) Msg.From = New MailAddress("me@test.com"); Msg.Subject = "test message" Msg.Text = "Hello!" ' Append this message to INBOX. This returns the ImapMessage object representing the new message. Dim IMsg = Imap1.CurrentMailbox.Messages.Add(Msg) | |
Copy the new message to another mailbox
A copy can be performed one of two ways. If you would like to copy multiple messages (perhaps all messages in a mailbox) you can use the MailBox.Copy or MailBox.BeginCopy methods of the Mailbox object. If you only need to copy a single message, use the ImapMessage.CopyTo or ImapMessage.BeginCopyTo methods of the ImapMessage object. All of these methods will result in two messages, the original and the new message. This is the case here. NOTE: The following code assumes there is a mailbox called "MyBox" already on the server. Also, because "INBOX" had to be selected during the copy operation, "INBOX" will remain as Imap.CurrentMailBox.
| C# | Copy Code |
|---|---|
// Copy the message to MyBox. iMsg.CopyTo(imap1.Mailboxes["MyBox"]); | |
| Visual Basic | Copy Code |
|---|---|
' Copy the message to MyBox. IMsg.CopyTo(Imap1.Mailboxes("MyBox")) | |
Setting flags.
IMAP message flags can be set one of three ways:
- By setting the properties of the ImapMessage object (good if dealing with a single message).
- By using the Mailbox.Set or Mailbox.BeginSet methods of the Mailbox object (good if dealing with multiple messages).
- By specifying flags when APPENDing a message using the ImapMessageCollection.Add or ImapMessageCollection.BeginAdd method of the ImapMessage object.
For this example, we want to change the flags of the original message we just appended and made a copy of. Of course, we could have set the flags when using the ImapMessageCollection.Add method. This is just to demonstrate usage of the ImapMessage object.
| C# | Copy Code |
|---|---|
// Set the "Flagged" flag iMsg.Flagged = true; | |
| Visual Basic | Copy Code |
|---|---|
' Set the "Flagged" flag IMsg.Flagged = True | |
Searching for messages
Finally, searching is demonstrated. For this example, just return all messages with the "Flagged" flag set. This should return our demonstration message, along with any other "Flagged" messages.
| C# | Copy Code |
|---|---|
// Create the search parameters. ArrayList parameters = new ArrayList(); parameters.Add(new ImapSearchParameter(ImapCriterion.Flagged, "")); // Send the search command ImapMessage[] messages = imap1.CurrentMailbox.Search(parameters); // Check the messages returned. Our test message should be included. foreach(ImapMessage m in messages) Debug.WriteLine(m.Id); | |
| Visual Basic | Copy Code |
|---|---|
' Create the search parameters. Dim Parameters As New ArrayList(); Parameters.Add(New ImapSearchParameter(ImapCriterion.Flagged, "")) ' Send the search command Dim Messages As ImapMessage() = Imap1.CurrentMailbox.Search(parameters) ' Check the messages returned. Our test message should be included. Dim M As ImapMessage For Each M in Mssages Debug.WriteLine(M.Id) Next | |