Sending an email using AddressUsed attribute of ActivityParty entity in Dynamics 365


When it comes to sending an email in Dynamics 365 / CE / CRM, we specify the entity’s record (with email enabled) in to, cc and bcc field of the email activity entity, and then the email address attribute of that particular record is used.

We can enable email for a custom entity also.

What if we have multiple email address fields specified for a custom entity?

The field which was created first will be used.

Check the below blog post –

https://debajmecrm.com/making-an-entity-enabled-to-send-emails-in-dynamics-365-and-its-existing-email-fields-gotchas/

What about entity like contact and account which have email, email address 2 and email address 3 fields?

If we trying sending an email to the above contact record, which has email address fields as blank programmatically we will get the below error

“Could not find the email address for the recipient of type ‘Contact’ with ID…”

What if we only have the email address 3 field having email id specified and the other 2 email field are blank?

This time our code will run without error, and CRM will pick up the email id specified in the email address 3 field. It will first check for email, then email address 2, and then finally email address 3 field. This happens only in the case of out of the box entity like contact and account.

What if we want to send an email to an email id not specified in the record?

Well in that case AddressUsed attribute of Activity Party can be used as shown below –

On running the above code, we can see the email specified in the addressused field being used

Understand more about activity party entity

https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg309626(v=crm.8)?redirectedfrom=MSDN

The helpful post

http://mostlymscrm.blogspot.com/2012/11/sending-emails-to-specific-address.html

Hope it helps..

string ConnectionString = "AuthType = OAuth; " +
"Username = userid;" +
"Password = pwd; " +
"Url = https://mydev.crm.dynamics.com/;" +
"AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;" +
"RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;" +
"LoginPrompt=Auto";

            CrmServiceClient svc = new CrmServiceClient(ConnectionString);

            if (svc.IsReady)
            {
                
                // guid of the contact record 
                Guid contactID = new Guid("0bd90832-bb52-eb11-bb23-000d3a5696d2");

                // initializing activity party entity
                Entity fromAP = new Entity("activityparty");
                Entity toAP = new Entity("activityparty");
                fromAP["partyid"] = new EntityReference("systemuser", new Guid("7de293e1-8352-eb11-bb23-000d3a569919"));
                toAP["partyid"] = new EntityReference("contact", contactID);

                // using the addressused attribute
                toAP["addressused"] = "nishant.rana.bliss@gmail.com";

                // create email record
                Entity email = new Entity("email");
                email["from"] = new Entity[] { fromAP };
                email["to"] = new Entity[] { toAP };
                email["regardingobjectid"] = new EntityReference("contact", contactID);
                email["subject"] = "Sample Subject";
                email["description"] = "This is sample description.";             
                Guid emailId = svc.Create(email);

                // Send email
                SendEmailRequest sendEmailRequest = new SendEmailRequest
                {
                    EmailId = emailId,                   
                    IssueSend = true
                };

                SendEmailResponse sendEmailResponse = (SendEmailResponse)svc.Execute(sendEmailRequest);

            }
Advertisements

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

2 thoughts on “Sending an email using AddressUsed attribute of ActivityParty entity in Dynamics 365”

  1. Hi,

    Why do we need below line of code if we are only setting Email address which are unresolved.
    toAP[“partyid”] = new EntityReference(“contact”, contactID);

    It does not make sense.

    I have tried without this line of code and it works fine.

    Thanks,
    Bipin

    Liked by 1 person

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.