Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / security

Set Up Properties of EmailMessage – Updated

5.00/5 (3 votes)
21 Feb 2022CPOL 4.5K  
How to update EmailMessage properties that don't have public setters
In this post, you will find a solution that contains a console application EWSMailMessage and a test library EWSMailMessage.Tests. EmailMethods class is a bit reworked to allow individual update of each of the mentioned properties.

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 individual update of each of the mentioned properties:

C#
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:

C#
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».

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)