How to obtain account information

This tutorial illustrates how to obtain Mailbox information with a few lines of code in Ultimate IMAP Component. Other examples for SMTP and POP3 can also be found at SMTP blog and POP3 blogThe simplest way to obtain information about a specified mailbox is using the GetMailboxInfo method. You only need to pass the mailbox name to the method. It returns a Mailbox object containing information about the mailbox you have requested.

If you want to obtain mailbox information of a Gmail IMAP account, please ensure the imap client connection is enabled on the GMAIL account setting page as shown in this picture:

IMAP Settings in GMAIL

The following steps illustrate how to obtain mailbox information with Ultimate mail library:

  1. Add using directives to your code to create aliases for existing ComponentSoft namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:


    C#

    using ComponentSoft.Net;
    using ComponentSoft.Net.Mail;

    VB.NET

    Imports ComponentSoft.Net
    Imports ComponentSoft.Net.Mail

  2. Create a new instance of the ImapClient class and call the Connect methods. The code looks similar to the following:
    C#

    // Create a new instance of the ImapClient class.
    ImapClient client = new ImapClient();
    // Connect to the server.
    client.Connect("myserver");
    // Or you can specify the IMAP port with
    // client.Connect("myserver", 143);
    // Login to the server.
    client.Authenticate("user", "password");
    // Get information about the INBOX mailbox.
    Mailbox m = client.GetMailboxInfo("INBOX");
    Console.WriteLine(
    "The number of recent messages: " + m.RecentMessages);
    Console.WriteLine(
    "The number of new unseen messages messages: " + m.NewUnseenMessages);
    // Close the connection.
    client.Disconnect();

    VB.NET

    ' Create a new instance of the ImapClient class.
    Dim client As New ImapClient()
    ' Connect to the server.
    client.Connect("myserver")
    ' Or you can specify the IMAP port with
    ' client.Connect("myserver", 143);
    ' Login to the server.
    client.Authenticate("user""password")
    ' Get information about the INBOX mailbox.
    Dim m As Mailbox = client.GetMailboxInfo("INBOX")
    Console.WriteLine("The number of recent messages: " & m.RecentMessages)
    Console.WriteLine("The number of new unseen messages messages: " & m.NewUnseenMessages)
    ' Close the connection.
    client.Disconnect()

Now you have successfully connected to the IMAP server and obtained information about the mailbox on the server. This is just a basic steps to manipulate mailboxes and messages. For more advanced tutorials, you can download the Ultimate Mail Library and take a look at the documentation as well as a number of carefully designed sample projects.

How to Downloading Mail Message Synchronously

Downloading mail messages from a IMAP server in UltimateMail component is so simple, you just call the DownloadMailMessage method and you retrieve an instance of the MailMessage class. You can now read, modify, save to disk the message. If you just need to download the message to disk or write it into a stream, just call the convenient DownloadMessage method instead.

The example below shows you how to get a list of messages from a IMAP server and download and save all messages to disk:

C#

// Select 'INBOX' mailbox
 client.Select("INBOX");
 // Get the message list.
 Console.WriteLine("Getting message list...");
 ImapMessageCollection list = client.ListMessages(EnvelopeParts.UniqueId | EnvelopeParts.Size);
 // Get messages.
 for (int i = 0; i < list.Count; i++)
 {
 ImapMessage imapMessage = list[i];
 // Download the message to an instance of the MailMessage class.
 MailMessage msg = client.DownloadMailMessage(imapMessage.UniqueId);
 // Display some information about it.
 Console.WriteLine("Size: " + imapMessage.Size);
 Console.WriteLine("Number of attachments: " + msg.Attachments.Count);
 Console.WriteLine("Number of header name value pairs: " + msg.Headers.Count);
 }

VB.NET

' Select 'INBOX' mailbox
 client.Select("INBOX")
 ' Get the message list.
 Console.WriteLine("Getting message list...")
 Dim list As ImapMessageCollection = client.ListMessages(EnvelopeParts.UniqueId Or EnvelopeParts.Size)
 ' Get messages.
 For i As Integer = 0 To list.Count - 1
 Dim imapMessage As ImapMessage = list(i)
 ' Download the message to an instance of the MailMessage class.
 Dim msg As MailMessage = client.DownloadMailMessage(imapMessage.UniqueId)
 ' Display some information about it.
 Console.WriteLine("Size: " & imapMessage.Size)
 Console.WriteLine("Number of attachments: " & msg.Attachments.Count)
 Console.WriteLine("Number of header name value pairs: " & msg.Headers.Count)
 Next i

After downloading messages from the server, you may want to obtain more information about the mailbox such as the number of unread messages and mailbox size, and then upload some messages to the server. See topic Obtaining account information for more details how to use the GetMailboxInfo method to get the account information.