Microsoft Exchange Server is a platform that lets you manage contacts, emails, calendars, schedules and provides several collaboration features. With MS Exchange Server, you can deploy your own mail server without relying on third parties. MS Exchange Server can also be accessed from within your web, desktop, or mobile applications via web services. Thus, you can perform the mailing and calendering operations in your applications. In this article, you are going to learn how to connect to an Exchange Server and read emails using C#.
- C# Exchange Server API
- Connect to MS Exchange Server
- Read Emails from Exchange Server using EWS
- Read Emails from Exchange Server using IMAP
C# Exchange Server API – Free Download
Aspose.Email for .NET provides easy to use API in order to work with MS Exchange Server. It lets you connect to your mail server using Exchange Web Service (EWS) or Internet Message Access Protocol (IMAP). Once you are connected, you can access items in a mailbox within a few steps. You can install Aspose.Email for .NET via NuGet or download its DLL.
PM> Install-Package Aspose.Email
Connect to an Exchange Server using C#
Aspose.Email for .NET provides two ways to connect to an instance of Exchange Server: EWS and IMAP. The following sections provide the steps of how to establish a connection using either of the ways.
Connect to an Exchange Server via EWS
The following are steps to connect to an MS Exchange Server using Aspose.Email for .NET.
- Use NetworkCredential class to set credentials including username, password, and domain.
- Get IEWSClient object returned by EWSClient.GetEWSClient(String, ICredentials) method.
- Use IEWSClient object to perform further operations.
The following code sample shows how to connect to Exchange Server via EWS using C#.
Connect to an Exchange Server via IMAP
In order to connect to an Exchange Server via IMAP, make sure that IMAP services are enabled on your server. Once done, follow the below steps to connect to the Exchange Server.
- Create an instance of ImapClient class and provide host, username, and password to its constructor.
- Set security options using ImapClient.SecurityOptions property.
- Access the items in the mailbox using the ImapClient object.
The following code sample shows how to access Exchange Server via IMAP using C#.
Read Emails from Exchange Server using EWS in C#
The following are the steps to read email messages from Exchange Server using EWS.
- Create an instance of the IEWSClient.
- Use IEWSClient.ListMessage(String) method to access the email collection.
- Loop through the email collection to get details of each email message.
The following code sample shows how to read emails from Exchange Server in C#.
Read Emails from Exchange Server using IMAP
The following are the steps to read emails from Exchange Server using IMAP.
- Create an instance of ImapClient class and provide host, username, and password to its constructor.
- Set security options using ImapClient.SecurityOptions property.
- Select a folder such as inbox, sent, etc using ImapClient.SelectFolder() method.
- Use ImapClient.ListMessages() method to get the email collection.
- Loop through the email collection to access each message.
The following code sample shows how to read emails from Exchange Server using IMAP.
Get a Free API License
You can try the API without evaluation limitations for free. Get a free temporary license now.
Live Demo
Conclusion
In this article, you have learned how to connect to MS Exchange Server and read email messages using C#. Furthermore, the code samples have shown how to access Exchange Server using EWS and IMAP clients. You can explore more about the C# Email API using documentation.