目录
背景
解决方案使用C#、.NET Framework 4.7.2、EWS 2.2、NUnit、FluentAssertions。
解决方案
解决方案包含一个控制台应用程序EWSMailMessage和一个测试库EWSMailMessage.Tests。EmailMethods类进行了一些修改,以允许对提到的每个属性进行单独更新:
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