Read emails from Exchange Server

Today I am going to write about exchange server. Last week I was working on a project and get a challenge to read emails from a mailbox on exchange server. It’s a new thing for me to read emails from exchange server. After digging out on the web I am able to read folders from a mailbox on exchange server. I thought it’s a good thing topic and let share my findings.
First of all to read emails from Exchange server you need download the SDK provided by Microsoft. It’s freely available and you can download from the following link,

After downloading and installing the Exchange server API, open a new Console Application in your Visual Studio. I am using Visual Studio 2012.
In the project add the following assembly reference.

I created a new method and name is ReadEmail. The first step will is to create an ExchangeService object. And select the Exchange server version you are using. In my case it’s 2010.

Next we need to provide the user name, password and domain name. You must provide the UserName who is having access to this mailbox.

Then we have to provide the Exchange WebService URL.

Now we are ready to read emails from the Inbox. Let’s assume I want to read all the emails from my Inbox, I am not going to place any filter. So I created object of FolderId and mention the folder name (WellKnownFolderName.Inbox) that I want to read from Inbox and also provide the email address from where I am going to read emails. As I am not using filter to read emails and so I will add one check to read only 10 emails from the server.

Now we are good to go to read emails. So in the next step we will loop through the emails and read its subject and body.

If you note I add a line msg.Load in the loop. This is important to load the message before reading its detail. If you will not load the message the application will throw an error.

You can add filter, so that emails which fulfill the filter criteria will be fetched from the exchange server. Like in the below example I am going to read only those emails which contains “Hello” keyword in there subject line.

Similarly you can send emails from Exchange Server. Below example explain how we can send an email and save a copy in the sent Item folder. First we are creating an EmailMessage object and pass the service object to it. And then creating a folder of SentItems, where we are going to save our sent email messages.

To delete an email message we need to call the Delete method of EmailMessage class. And we need to pass the DeletedMode. Like in the below example I am going to delete a message and move that message to deleted folder.

I attached the source code, hope this will give you a basic overview of reading emails from exchange server. To get more details about exchange server you will find it here.

You can download code from here,



Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *