PowerTCP Mail for .NET
Getting from the Default Mailbox
Send comments on this topic.



Glossary Item Box

Accessing an IMAP server with the Imap component is both quick and easy.  A typical scenario will include logging into a server,  accessing the default "INBOX" mailbox, getting the message headers from the messages in the mailbox, displaying the header, and logging out of the server.

To quickly and easily get all messages.

  1. Add the Imap component to a new form.
  2. Import the namespace of the component you would like to use using "Using" (C#) or "Imports" (VB.NET). Place the following code at the top of your file.
    C# Copy Code
    using Dart.PowerTCP.Mail;
    Visual Basic Copy Code
    Imports Dart.PowerTCP.Mail
  3. Add a button to the form.
  4. Login to the server.
    C# Copy Code
    // Login to the IMAP server.
    imap1.Login("mail.myserver.com", "test", "pass");
    Visual Basic Copy Code
    ' Login to the IMAP server.
    Imap1.Login("mail.myserver.com", "test", "pass")
  5. At this point, if AutoList is True (by default), a List command is sent to the server immediately after the login occurs, returning all the mailboxes and the number of messages contained in each mailbox.  The default current mailbox is "INBOX" as that is the most commonly used mailbox on mail servers.  The easiest way to access messages on the server is to call the MailBox.Get method.  When the method is used with no parameters all messages are retrieved from the server, but this can be a lengthy operation if there are numerous messages in the mailbox.  Use MailBox.Get with overloads to specify which messages and message items to retrieve.
    C# Copy Code
    imap1.CurrentMailbox.Get();
    Visual Basic Copy Code
    Imap1.CurrentMailbox.Get()
  6. The retrieved messages are represented as ImapMessage objects. To access each message, either access an index into the MailBox.Messages collection, or access each message using For...Each.  Access header data. For this example, do something trivial like display the headers for each message and when finished logout.
    C# Copy Code
    // Iterate through the messages
    foreach(ImapMessage msg in imap1.CurrentMailbox.Messages)
       // Display header info about each Message
       Debug.Write(msg.Message.Header.ToString());
    imap1.Logout();
    Visual Basic Copy Code
    Dim Msg As ImapMessage
    For Each Msg In imap1.CurrentMailbox.Messages
       ' Display info about each Message
       Debug.Write(Msg.Message.Header.ToString())
    Next
    Imap1.Logout()
Documentation Version 3.2
© 2010 Dart Communications. All Rights Reserved.