Home > Code > C# > Connect to MS Exchange Server & List Messages from Inbox Folder using IMAP

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

by sher azam   on Mar 19, 2014   Category: C#   |  Views: 1505    |  Points: 25   |  Gold 


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.

Below is the sample source code to connect and list messages from Inbox folder of Microsoft Exchange Server using IMAP protocol.

[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);
}



Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts