jimminybob
jimminybob

Reputation: 1442

Get Recipients List from Email C#

I'm creating a plugin and I want to check if the recipients to an email are of a certain type. I've found the "to" is an ActivityParty type and I cant seem to get the individual recipients from the list. Can anyone help?

Upvotes: 2

Views: 4918

Answers (2)

jimminybob
jimminybob

Reputation: 1442

I managed this using the code below:

EntityCollection Recipients = email.GetAttributeValue<EntityCollection>("to");

foreach (var party in Recipients.Entities)
{  
var partyName = party.GetAttributeValue<EntityReference>("partyid").Name;
var partyId = party.GetAttributeValue<EntityReference>("partyid").Id;

…
}

Upvotes: 1

lazarus
lazarus

Reputation: 2017

Actually 'To' is list of ActivityParty entities. Every ActivitParty object contains PartyId property which is EntityReference. So, if you want to find entity type of email receiver(or receivers) try with next code:

Email email;
...
IEnumerable<ActivityParty> emailRecievers = email.To;
foreach (ActivityParty ap in emailRecievers)
{
    string entityTypeName = ap.PartyId.LogicalName;
    if (entityTypeName == "contact")
    {
        // do something...
    }
}

Upvotes: 3

Related Questions