Click here to Skip to main content
15,867,686 members
Articles / Security

Set Up Properties of EmailMessage – Updated

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
21 Feb 2022CPOL 4K   2   1
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)


Written By
Software Developer (Senior)
Ukraine Ukraine
• Have more than 25 years of the architecting, implementing, and supporting various applications from small desktop and web utilities up to full-fledged cloud SaaS systems using mainly Microsoft technology stack and implementing the best practices.
• Have significant experience in the architecting applications starting from the scratch and from the existent application (aka “legacy”) where it is required to review, refactor, optimise the codebase and data structure, migrate to new technologies, implement new features, best practices, create tests and write documentation.
• Have experience in project management, collecting business requirements, creating MVP, working with stakeholders and end users, and tasks and backlog management.
• Have hands-on experience in the setting up CI/CD pipelines, the deploying on-premise and cloud systems both in Azure and AWS, support several environments.
• As Mathematician, I interested much in the theory of automata and computer algebra.

Comments and Discussions

 
GeneralMy vote of 5 Pin
LightTempler22-Feb-22 9:13
LightTempler22-Feb-22 9:13 
Thanks for!
LiTe

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.