What is EWS?

July 6, 2020

Exchange Web Services is an Application Program Interface (API) by Microsoft that allows programmers to fetch Microsoft Exchange items including calendars, contacts and emails. It can be used to read the email box and retrieve emails along with all the metadata such as headers, body and attachments. This is useful when the same information needs to be extracted from Exchange items repeatedly. The example I’m using in this article is retrieval of order details from emails, and is based on a recent assignment for a client at MetaSys.

How does it help?

The client wanted the ability to read the email inbox, import the order to the system and then send the email with the order details generated to the sales representatives.

With regular mail reading API’s the process would have been as follows. First the email is read, secondly the order is imported, and finally a new function is called that sends a new email with new order details to the sales representative. In this case the details of the requester are saved and reused for sending the confirmation email.

Using the EWS managed API, a more efficient solution was developed. The email was directly forwarded from the email inbox, without the need to create a separate email for the order confirmation. The confirmation email is created directly from the received email object, as is the forwarded email to the sales representative. The following code sample shows how the forwarded email is created and sent:

ResponseMessage responseMessage = message.CreateForward();
responseMessage.ToRecipients.Add(email.FromAddress);
responseMessage.BodyPrefix = messageBodyPrefix;
responseMessage.Send();

In the code snippet above, “message” is the object which contains all the details of the order email and we use it to create the new forward email without saving any details to the local system or variables.

Similarly, we can use reply functionality of the API to maintain the email conversation chain by using the following code:

bool replyToAll = true;
ResponseMessage responseMessage = message.CreateReply(replyToAll);

string emailbody= “Please find the attachment below.”;
responseMessage.BodyPrefix = emailbody;
responseMessage.Send();

Setting the “replyToAll” variable to true specifies that mail will be forwarded to all the recipients who were present in the original conversation. The text contained in the variable “emailbody” will be on the top of the email body of the conversation.

Additional features of EWS managed API

EWS provides useful features for dealing with emails with invalid delivery email addresses. The postmaster bot may send a mail delivery failure email to the same inbox, which can cause issues with the importing of other orders. These issues can be resolved in EWS by checking the subject lines, and automatically deleting delivery failure emails, or moving them to a separate folder. These orders can then be separately corrected and resent without interfering with the remaining orders in the inbox.

The following code sample can be used to move all the email items from the inbox to the “DidNotDelivered” folder:

Folder rootfolder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot);
rootfolder.Load();
foreach (Folder folder in rootfolder.FindFolders(new FolderView(100)))
{
FindItemsResults findResults =
service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
// The if below checks if the folder “DidNotDelivered” is present in the email box
if (folder.DisplayName == “DidNotDelivered”)
{
var fid = folder.Id;
foreach (Item item in findResults.Items)
{
item.Load();
item.Move(fid);
}
}
}

This is how EWS helped us simplify the processing of emails with minimal lines of code.

We hope that this article gives you some useful ideas for dealing with Microsoft Exchange using EWS. If you are having issues, feel free to get in touch with us at https://www.metasyssoftware.com/dot-net

Leave a Comment

Tags :

Category :