To authenticate to an IMAP server, you can follow the steps below
- Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
using System;
using System.Text;
using ComponentPro.Net.Mail;
Imports System.Text
Imports ComponentPro.Net.Mail
- Create a new instance of the Imap class.
- Now you can connect to the IMAP server with Connect methods. The code looks similar to the following:
// Create a new instance of the Imap class.
Imap client = new Imap();
// Connect to the server.
client.Connect("myserver");
// Or you can specify the IMAP port with
// client.Connect("myserver", 143);
' Create a new instance of the Imap class.
Dim client As New Imap()
' Connect to the server.
client.Connect("myserver")
' Or you can specify the IMAP port with
' client.Connect("myserver", 143);
- Use your user name and password to login with one of Authenticate methods. The code looks similar to the following:
// Login to the server.
client.Authenticate("user", "password");
' Login to the server.
client.Authenticate("user", "password")
- Do your work like listing folders, downloading/uploading mail messages ,etc. The code looks similar to the following:
StringBuilder sb = new StringBuilder();
FolderCollection list = client.ListFolders();
for (int i = 0; i < list.Count; i++)
{
sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].Name);
}
Console.WriteLine(sb.ToString());
Dim sb As New StringBuilder()
Dim list As FolderCollection = client.ListFolders()
For i As Integer = 0 To list.Count - 1
sb.AppendFormat("{0} - {1}" & vbCrLf, i + 1, list(i).Name)
Next i
Console.WriteLine(sb.ToString())
- After completing your work, call the Disconnect method to close the IMAP session.
Final example code
using System;
using System.Text;
using ComponentPro.Net.Mail;
...
// Create a new instance of the Imap class.
Imap client = new Imap();
// 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");
StringBuilder sb = new StringBuilder();
FolderCollection list = client.ListFolders();
for (int i = 0; i < list.Count; i++)
{
sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].Name);
}
Console.WriteLine(sb.ToString());
// Close the connection.
client.Disconnect();
Imports System.Text
Imports ComponentPro.Net.Mail
...
' Create a new instance of the Imap class.
Dim client As New Imap()
' 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")
Dim sb As New StringBuilder()
Dim list As FolderCollection = client.ListFolders()
For i As Integer = 0 To list.Count - 1
sb.AppendFormat("{0} - {1}" & vbCrLf, i + 1, list(i).Name)
Next i
Console.WriteLine(sb.ToString())
' Close the connection.
client.Disconnect()