C# – Read emails from exchange online mailbox (Office 365) into SQL Server

C# – Read emails from exchange online mailbox (Office 365) into SQL Server

Situation

You have an Office 365 mailbox. You want to use C# to read those mails and save them to SQLServer.

Requirements

  • Office365 mailbox
  • SQLServer
  • Exchange WebServices
  • C# (visual studio)

Exchange WebServices

Applies to: EWS Managed API | Exchange Online | Exchange Server 2013 | Office 365

There is 1 step you need to take and that is to install Exchange Webservices (MIT License).

Download EWS Managed API via nuget.

This step is very easy and does not require more then download/install via a nuget package. This only has to be done on your Development Machine.

Once you want to release your product for production release all you need to include is the Microsoft.Exchange.WebServices.dll and you’re all set.

Implemented Solution

 

Instantiate a new ExchangeService and provide your mailbox credentials.

Then, because you want to call an Webservice (ExchangeWebService EWS) you provide the webservice URL and you call the function FindItems( , ).
This call will connect to your mailbox (if you’ve provided the correct credentials) and start reading (in this case 100 mail headers)

After that you can read all the basic email properties like subject, from and to. Microsoft has implemented a lightweight EWS so that if you want to read more properties like Htmlbody or TextBody, you have to send an instruction to load that extra information with the following command:

Save your emails to the database

The complete visual studio solution can be downloaded here. ReadOffice365MailAndSaveToSQL

About us

Over the years, SQLTreeo gained precious know-how which we transformed into customer support, managed database services and database solutions.

In order to express our knowledge and experiences, we tend to work with anything less than the best-trained staff and that is the key to success and the biggest asset of our company. Thus, all our database management services are controlled by high level skilled and Senior database administrators.

We can help you in every aspect of SQL Server.

Danny Riebeek

Danny is a SQL Enthusiast, always disovering new ways to leverage SQL Server functionality

View my other posts

Leave a Reply