Introduction
In this article, we will explore how to read an email in Outlook and add the content of the email body to a SharePoint List. We can achieve this by writing a C# console application using Microsoft Exchange Web Services.
Scenario
If we talk about a requirement, let’s take a part of an HR Portal in which an HR sends some information about newly joined employees to, let’s say, admin team via an E-mail. Then, the admin team stores the information of each employee in the SharePoint List manually.
Solution
We are writing a console application here where every email with Employee details will be creating an item in the SharePoint List and the fields of the item will be obtained from the email body. Make sure, the format of the email body is structured as demonstrated below in the last image.
The console application can be scheduled via Windows Scheduler to run every 5 minutes to check the incoming emails in Outlook.
Please follow the below steps for more detailed information.
Prerequisites
Make sure that you have Microsoft Exchange Web Services installed and running. If you do not have Exchange Web Services, you might get an error during compilation.
Follow the below steps for installation:
Step 1
Once you have created a console application and added all the required references, right click on References and click "Manage NuGet Packages…"
Step 2
It will open the NuGet Package Manager. Search for “Microsoft.Exchange.WebServices” and when found, select those and click Install.
Code Implementation
Create the object of the Exchange Service and provide your Exchange Service credentials. Link your Mailbox with the Exchange Service Object.
- ExchangeService oews = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
- {
- Credentials = new WebCredentials("akashkumhar","password123","domainname")
- }; oews.AutodiscoverUrl("[email protected]");
Here, I have created two folders under Inbox - LetterBox and SuccessfulItems.
- LetterBox - It contains all the Emails which are to be parsed and processed.
- SuccessfulItems - It contains all the Emails for which the corresponding items are created successfully in the SharePoint List.
The application is designed in such a way that when you send an email to the linked Exchange Email ID, the Inbox rule moves the email received with subject “EmailReader” to the LetterBox so, that the intended emails are only parsed by the code and not the emails in the Inbox.
The console application reads all the unread emails from LetterBox, creates the items in SharePoint list, and moves them to SuccessfulItems.
Make sure the email body is standard for all the emails, as shown below.
I have created an Employee list with the below columns and data types for demonstration.
The LetterBox is parsed for all the unread emails and the data is picked from Email body to be added into the corresponding columns.
I have done a successful implementation of this functionality. Check the below-created list items.
If I go ahead and show you the "Edit" form of one item, you could see how the field values are correctly placed at their respective fields.
When you deal with a "Choice" column which is a Checkbox with multiple option values, then you need to handle it differently than other columns. This will consider each option separated by commas as different and mark the checkboxes correctly.
NOTE - Please refer the entire code in the attachment.
I hope this article was helpful to you.