Set up properties of EmailMessage by EWS 2.0

I am writting client application that uses Exchange Web Services managed API v.2.0 in order to connect to Exchange Server. Sometimes, I need create EmailMessage object and make it looks like as received letter. Therefore I need set up such properties of EmailMessage as ReceiveBy, DateTimeCreate, DateTimeReceived, but they haven’t public set assessors. Using ILDasm utility we find out that values of these properties are stored in the inner dictionary PropertyBag. So the values of necessary properties can be set up by using reflection.
The code below shows this workaround. By the way this code doesn’t connect to Exchange Server, so ExchangeService object may have fake Url property.

using System;
using Microsoft.Exchange.WebServices.Data;

namespace Exchange.Mail
{
    public static class EmailMethods
    {
        public static EmailMessage CreateEmailMessage(ExchangeService service,
             string subject,
             string body,
             string address)
        {
            if (service == null)
                return null;

            var message = new EmailMessage(service)
                {
                    Subject = subject,
                    Body = body,
                    ItemClass = "IPM.Note",
                    From = null
                };
            SetProperty(message, EmailMessageSchema.ReceivedBy, new EmailAddress(address));
            SetProperty(message, ItemSchema.DateTimeCreated, DateTime.Now.AddMinutes(-10));
            SetProperty(message, ItemSchema.DateTimeReceived, DateTime.Now);

            return message;
        }

        static 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;
            if (dictionary == null)
                return false;

            dictionary[propertyDefinition] = value;
            return true;
        }

        static 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());
        }
    }
}

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

11 thoughts on “Set up properties of EmailMessage by EWS 2.0

    Добавить комментарий

    Ваш адрес email не будет опубликован. Обязательные поля помечены *

    Этот сайт использует Akismet для борьбы со спамом. Узнайте, как обрабатываются ваши данные комментариев.