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.
- Add the Imap component to a new form.
- 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 Codeusing Dart.PowerTCP.Mail;Visual Basic
Copy CodeImports Dart.PowerTCP.Mail - Add a button to the form.
- 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")
- 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 Codeimap1.CurrentMailbox.Get();
Visual Basic
Copy CodeImap1.CurrentMailbox.Get()
- 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 CodeDim Msg As ImapMessage For Each Msg In imap1.CurrentMailbox.Messages ' Display info about each Message Debug.Write(Msg.Message.Header.ToString()) Next Imap1.Logout()