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 in C#. Also, we will demonstrate how to read emails from Exchange Server in C#.
- C# API to Read Emails from Exchange Server
- Connect to MS Exchange Server in C#
- Read Emails from Exchange Server using EWS
- Read Emails from Exchange Server using IMAP
C# API to Read Emails from Exchange Server - 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#.
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#.
C# Read Emails from Exchange Server using EWS
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 in C#
The following are the steps to read emails from Exchange Server using IMAP in C#.
- 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 C# code sample shows how to read emails from Exchange Server using IMAP.
C# API to Read Exchange Server’s Emails - Get a Free License
You can try the API without evaluation limitations for free. Get a free temporary license now.
Aspose Email API for C# - Live Demo
Conclusion
In this article, you have learned how to connect to MS Exchange Server in C#. In addition, we also covered how to read emails from Exchange Server using C#. The code samples have explicitly shown how to access Exchange Server using EWS and IMAP clients. You can explore more about the C# Email API using documentation.
See Also
Create and Send Emails using C# | Read Emails using IMAP in Python | Connect to SMTP Server using Python |
Group Messages from PST by Conversation Threads using C# .NET |