Set up properties of EmailMessage – Updated

Source code

Introduction

This post is an update of the post Set up properties of EmailMessage, published in 2014. It still uses EWS v2.2.0, but according to the post

Starting July 19th 2018, Exchange Web Services (EWS) will no longer receive feature updates. While the service will continue to receive security updates and certain non-security updates, product design and features will remain unchanged.

Background

Solution uses C#, .Net Framework 4.7.2, EWS 2.2, NUnit, FluentAssertions.

Solution

Solution contains a console application EWSMailMessage and a test library EWSMailMessage.Tests. EmailMethods class is a bit reworked to allow individually update each of the mentioned properties:

public class EmailMethods : IEmailMethods
{
	public EmailMessage CreateEmailMessage(ExchangeService service,
			string subject,
			string body,
			string address)
	{
		if (service == null ||
			string.IsNullOrWhiteSpace(subject) ||
			string.IsNullOrWhiteSpace(address))
			return null;

		var emailMessage = new EmailMessage(service)
		{
			Subject = subject,
			Body = body,
			ItemClass = "IPM.Note",
			From = address
		};

		return emailMessage;
	}

	public void SetReceivedBy(EmailMessage emailMessage, string address)
	{
		if (emailMessage == null ||
			string.IsNullOrWhiteSpace(address))
			return;
		SetProperty(emailMessage, EmailMessageSchema.ReceivedBy, new EmailAddress(address));
		return;
	}

	public void SetDateTimeCreated(EmailMessage emailMessage, DateTime dateTime)
	{
		if (emailMessage == null)
			return;
		SetProperty(emailMessage, ItemSchema.DateTimeCreated, dateTime);
		return;
	}

	public void SetDateTimeSent(EmailMessage emailMessage, DateTime dateTime)
	{
		if (emailMessage == null)
			return;
		SetProperty(emailMessage, ItemSchema.DateTimeSent, dateTime);
		return;
	}

	public void SetDateTimeReceived(EmailMessage emailMessage, DateTime dateTime)
	{
		if (emailMessage == null)
			return;
		SetProperty(emailMessage, ItemSchema.DateTimeReceived, dateTime);
		return;
	}

	#region Set property

	private bool SetProperty(EmailMessage message,
			PropertyDefinition propertyDefinition,
			object value)
	{
		if (message == null)
			return false;

		// get value of PropertyBag property – that is wrapper
		// over dictionary of inner message’s properties
		var members = message.GetType().FindMembers(
			MemberTypes.Property,
			BindingFlags.NonPublic | BindingFlags.Instance,
			PartialName,
			"PropertyBag");
		if (members.Length < 1)
			return false;

		var propertyInfo = members[0] as PropertyInfo;
		if (propertyInfo == null)
			return false;

		var bag = propertyInfo.GetValue(message, null);
		members = bag.GetType().FindMembers(
			MemberTypes.Property,
			BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
			PartialName,
			"Properties");
		if (members.Length < 1)
			return false;

		// get dictionary of properties values
		var properties = ((PropertyInfo)members[0]).GetMethod.Invoke(bag, null);
		var dictionary = properties as Dictionary<PropertyDefinition, object>;
		if (dictionary == null)
			return false;

		dictionary[propertyDefinition] = value;
		return true;
	}

	private bool PartialName(MemberInfo info, Object part)
	{
		// Test whether the name of the candidate member contains the
		// specified partial name.
		return info.Name.Contains(part.ToString());
	}

	#endregion
}

Program.cs demonstrates how to call methods:

var emailMethods = new EmailMethods();
var exchangeServer = new ExchangeService();
var emailMessage = emailMethods.CreateEmailMessage(
	exchangeServer, "New Subject", "Interesting text", "illya@ireznykov.com");

emailMethods.SetReceivedBy(emailMessage, "ews@example.com");
emailMethods.SetDateTimeCreated(emailMessage, DateTime.Now.AddDays(-1));
emailMethods.SetDateTimeSent(emailMessage, DateTime.Now.AddHours(-22));
emailMethods.SetDateTimeReceived(emailMessage, DateTime.Now.AddHours(-20));

1. All used IP-addresses, names of servers, workstations, domains, are fictional and are used exclusively as a demonstration only.
2. Information is provided «AS IS».

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.