DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 95 posts at DZone. View Full User Profile

Connect to MS Exchange Server & List Messages from Inbox using IMAP Protocol

03.19.2014
| 1918 views |
  • submit to reddit
//[C#]

// Connect to Exchange Server using ImapClient class
ImapClient imapClient = new ImapClient("ex07sp1", "Administrator", "Evaluation1");
imapClient.Connect(true);

// Select the Inbox folder
imapClient.SelectFolder(ImapFolderInfo.InBox);

// Get the list of messages
ImapMessageInfoCollection msgCollection = imapClient.ListMessages();
foreach (ImapMessageInfo msgInfo in msgCollection)
{
    Console.WriteLine(msgInfo.Subject);
}
// Disconnect from the server
imapClient.Disconnect();
 
//[VB.NET]

' Connect to Exchange Server using ImapClient class
Dim imapClient As ImapClient = New ImapClient("ex07sp1", "Administrator", "Evaluation1")
imapClient.Connect(True)

' Select the Inbox folder
imapClient.SelectFolder(ImapFolderInfo.InBox)

' Get the list of messages
Dim msgCollection As ImapMessageInfoCollection = imapClient.ListMessages()
For Each msgInfo As ImapMessageInfo In msgCollection
    Console.WriteLine(msgInfo.Subject)
Next msgInfo
' Disconnect from the server
imapClient.Disconnect()

//Programming Samples for SSL

//If the Exchange Server uses SSL, modify the above code as follows:

//[C#]

private void ConnectUsingSSL()
{
// Connect to Exchange Server using ImapClient class
ImapClient imapClient = new ImapClient("ex07sp1", 993, "Administrator", "Evaluation1", new RemoteCertificateValidationCallback(RemoteCertificateValidationHandler));
imapClient.EnableSsl = true;
imapClient.SecurityMode = ImapSslSecurityMode.Implicit;
imapClient.Connect(true);

lstLog.Items.Add("Connected to Exchange server");
// Select the Inbox folder
imapClient.SelectFolder(ImapFolderInfo.InBox);

// Get the list of messages
ImapMessageInfoCollection msgCollection = imapClient.ListMessages();
foreach (ImapMessageInfo msgInfo in msgCollection)
{
    Console.WriteLine(msgInfo.Subject);
}
// Disconnect from the server
imapClient.Disconnect();
}

// Certificate verification handler
private bool RemoteCertificateValidationHandler(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    return true; //ignore the checks and go ahead
}

//[VB.NET]

' Connect to Exchange Server using ImapClient class
Dim imapClient As ImapClient = New ImapClient("ex07sp1", 993, "Administrator", "Evaluation1", New RemoteCertificateValidationCallback(AddressOf RemoteCertificateValidationHandler))
imapClient.EnableSsl = True
imapClient.SecurityMode = ImapSslSecurityMode.Implicit
imapClient.Connect(True)

Console.WriteLine("Connected to Exchange server")
' Select the Inbox folder
imapClient.SelectFolder(ImapFolderInfo.InBox)

' Get the list of messages
Dim msgCollection As ImapMessageInfoCollection = imapClient.ListMessages()
For Each msgInfo As ImapMessageInfo In msgCollection
    Console.WriteLine(msgInfo.Subject)
Next msgInfo
' Disconnect from the server
imapClient.Disconnect()

' Certificate verification handler
Private Function RemoteCertificateValidationHandler(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As Net.Security.SslPolicyErrors) As Boolean
    Return True 'ignore the checks and go ahead
End Function
 
//Programming Sample - Saving One Message Only

//After connecting to an Exchange server using IMAP and getting the IMapMessageInfoCollection, you can use the MessageInfo object's sequence number to save a specific message as follow:

//[C#]

// Get the list of messages
ImapMessageInfoCollection msgCollection = imapClient.ListMessages();
foreach (ImapMessageInfo msgInfo in msgCollection)
{
    //Fetch the message from inbox using its SequenceNumber from msgInfo
    MailMessage msg = imapClient.FetchMessage(msgInfo.SequenceNumber);

    //Save the message to disc now
    msg.Save(msgInfo.SequenceNumber + ".msg", MailMessageSaveType.OutlookMessageFormat);
}
This technical tip explains how developers can connect and list messages from Inbox folder of Microsoft Exchange Server using IMAP protocol. Microsoft Exchange Server supports the IMAP protocol for accessing items in a mailbox. Use Aspose.Email's ImapClient class to connect to the Exchange Server using IMAP protocol. This article shows how. To connect to an Exchange Server using IMAP, please first, make sure that IMAP services are enabled for your Exchange Server:

  • Open the Control Panel.
  • Go to Administrator Tools, then Services.
  • Check the status of the Microsoft Exchange IMAP4 service.
  • If it is not already running, enable/start it.

The C# and VB.NET sample source code shows how to connect and list messages from Inbox folder of Microsoft Exchange Server using IMAP protocol.