设置EmailMessage的属性——更新

目录

背景

解决方案


背景

解决方案使用C#.NET Framework 4.7.2EWS 2.2NUnitFluentAssertions

解决方案

解决方案包含一个控制台应用程序EWSMailMessage和一个测试库EWSMailMessage.TestsEmailMethods类进行了一些修改,以允许对提到的每个属性进行单独更新:

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演示了如何调用方法:

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. 所有使用的IP地址、服务器名称、工作站、域都是虚构的,仅用作演示。
2. 信息按原样提供。

本文最初发布于Set up properties of EmailMessage – Updated – I.Reznykov's blog

https://www.codeproject.com/Articles/5325720/Set-Up-Properties-of-EmailMessage-Updated

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值