How to – Send 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 “How to – Send 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 )

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.

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

Two Bite Tips

Valuable Tips To Grow Your Business

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

Dreaming to be a clean coder and TDD minded programmer.

Transform 365

We blog about problems we face and code we write to help others

%d bloggers like this: