rhuebscher
rhuebscher

Reputation: 11

C# Service with Exchange EWS: Reading recipients from newly created reply

After creating a "reply to all" from an incoming email, I need to remove recipients that are not allowed to get this reply. But after creating the reply message, the recipients Lists (ToRecipients, CcRecipients, BccRecipients) of the reply message are empty.

ResponseMessage responseMessage = email.CreateReply(true);
foreach (EmailAddress toRecipient in responseMessage.ToRecipients)
{
    if (! _outboundEmailAdresses.Contains(toRecipient.Address))
    {
        responseMessage.ToRecipients.Remove(toRecipient.Address);
    }
}

`

If I iterate through email.ToRecipients, I see all the recipients. If I iterate through responseMessage.ToRecipients, I can't see any recipients. Shouldn't email.CreateReply(true) copy the email.from and email.ToRecipients addresses to the responseMessage?

Upvotes: 0

Views: 753

Answers (1)

Jason Johnston
Jason Johnston

Reputation: 17702

This one's a bit confusing. Basically the ResponseMessage class implements EWS's CreateItem operation with the ReferenceItemId element, which allows you to send minimal information to the server. The idea is the server already has most of the info it needs to send a reply (like the original body, recipient list, etc), so you don't need to re-send that information. So the ResponseMessage doesn't get a copy of the recipients from the EmailMessage, because it doesn't need it.

You'll want to check the recipients of the original message, plus the sender. If they don't contain an unwanted address, then you don't need to do anything. If they do, then you'll want to set ResponseMessage.ToRecipients. If you do this, you need to add all of the desired addresses. Touching ResponseMessage.ToRecipients overrides the original recipient list.

Upvotes: 2

Related Questions