How to send email to the secondary address of contact/account/lead entity

Dynamics CRM by default send email to the main address (emaildresse1)

I have created a plugin to send an email to recipient secondary addresses
Such as a contract that has multiple email addresses,
Its main address (emailadresse1) and  secondary address (emailadress2)and other personal email address (emailadress3).

In some cases we need to send an email to the different mail addresses, so to solve this problem I create two custom boolean fields in the entity email (emailaddress2, and emailaddress3) to choose if we want to send the email to secondary addresses
I have republished it as boxes to check as the screen prints below

email form
Email form

The addresses are marked in red in the cc field (they added automatic by our plugin), this is the default behavior of the CRM, because the CRM resolved the recipient name by its email address1.

Then I added a plugin to add the secondary addresses in the (cc) field, the following method adds the secondary addresses in the cc field

// this is function to check if the customize field is checked and add the specific email adderess to cc field
private static void AddAdressMailToCc(IOrganizationService service, Entity emailEntity, Entity activityParty, string entityName)
{
/****entity represent to recipient like account or contact or lead*****/
Entity entity = service.Retrieve(entityName, ((EntityReference)activityParty.Attributes["partyid"]).Id, new ColumnSet("emailaddress2", "emailaddress3"));
if (entity != null)
{
EntityCollection cc = new EntityCollection();
if (emailEntity.GetAttributeValue("new_adressemail2") == true)
{
if (entity.Contains("emailaddress2") && entity.Attributes["emailaddress2"] != null)
{
EntityCollection ec = emailEntity.GetAttributeValue("cc");
Entity party2 = new Entity("activityparty");
party2["addressused"] = (string)entity["emailaddress2"];
cc.Entities.Add(party2);
}
}
if (emailEntity.GetAttributeValue("new_adressemail3") == true)
{
if (entity.Contains("emailaddress3") && entity.Attributes["emailaddress3"] != null)
{
Entity party3 = new Entity("activityparty");
party3["addressused"] = (string)entity["emailaddress3"];
cc.Entities.Add(party3);
}
}
(emailEntity["cc"] as EntityCollection).Entities.AddRange(cc.Entities);
}
service.Update(emailEntity);
}

And I call this method in the Execute method

public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);
Guid emailId = Guid.Empty;
if (context.PrimaryEntityName != "email")
{
return;
}
if (context.InputParameters.Contains("EmailId") && context.InputParameters["EmailId"] is Guid)
{
emailId = (Guid)context.InputParameters["EmailId"];
if (emailId == Guid.Empty)
{
return;
}
Entity emailEntity = service.Retrieve("email", emailId, new ColumnSet(true));
if (emailEntity != null && emailEntity.Contains("to"))
{
EntityCollection activityParties = (EntityCollection)emailEntity.Attributes["to"];
foreach (Entity activityParty in activityParties.Entities)
{
//contact entity
if (activityParty.Contains("partyid") && ((EntityReference)activityParty.Attributes["partyid"]).LogicalName == "contact")
{
AddAdressMailToCc(service, emailEntity, activityParty, "contact");
}
//account entity
if (activityParty.Contains("partyid") && ((EntityReference)activityParty.Attributes["partyid"]).LogicalName == "account")
{
AddAdressMailToCc(service, emailEntity, activityParty, "account");
}
//lead entity
if (activityParty.Contains("partyid") && ((EntityReference)activityParty.Attributes["partyid"]).LogicalName == "lead")
{
AddAdressMailToCc(service, emailEntity, activityParty, "lead");
}
}
}
}
 And I add the plugin with RegistrationPluginTool :

  •  PreOperation
  • Message: send
  • Primary Entity : Email

Get description of optionSet

Enumeration :

public enum Country
{

[Display(Description=”Tunisia”)]
Tunisia = 1,

[Display(Description=”France”)]
France = 2,

[Display(Description=”UnitedState”)]
UnitedState = 3,
}

To get the description of enumeration :


public static string GetDescriptionFromEnumValue(Enum value)
{
if (value == null)
return null;
try
{
var fieldInfo = value.GetType().GetField(value.ToString());
if(fieldInfo == null)
{ return null;
}
DisplayAttribute attribute = fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false).SingleOrDefault() as DisplayAttribute;
return attribute.Description;
}
//if the value of enum doesn't exist
catch (Exception ex)
{
throw;
}
}