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

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 and If it is not already running, enable/start it. To View C# and VB.NET samples and other details please visit the main page.
//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); }

Url: http://www.aspose.com/.net/email-component.aspx

Language: C# | User: Sheraz Khan | Created: Mar 19, 2014 | Tags: Connect to an Exchange Server, list messages from Inbox folder, Microsoft Exchange Server, Connect to an Exchange using IMAP, Microsoft Exchange IMAP4 service, Exchange Server uses SSL Connect to an Exchange Server list messages from Inbox folder Microsoft Exchange Server Connect to an Exchange using IMAP Microsoft Exchange IMAP4 service Exchange Server uses SSL