Send an Email in Dynamics 365 CRM using C# Plugins

Tamilarasu Arunachalam
2 min readJun 25, 2023

--

Photo by Brett Jordan on Unsplash

We have all came through the activity timeline section in account and contact forms. The activities include Email, Phone call, Appointments, Tasks, Posts, and Notes. I have explored how to send an email inside Dynamics 365 CRM using plugins. It is quite simple by using the two pre-defined objects, which are SendEmailRequest and SendEmailResponse.

Note: The sender of the email should be a system user or queue. And the receiver might be a user, queue, account, contact, lead, entitlement, facility/equipment, or knowledge article.

The agenda of this article is to send a welcome email when a contact is created. The plugin is triggered when a contact record is created. Please refer to the below code for creating and sending the new email activity.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;

namespace CreateEmailActivity
{
public class CreateEmailActivity:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.InitiatingUserId);
ITracingService log = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

createEmailActivity(context, service, log);
}
public void createEmailActivity(IPluginExecutionContext context, IOrganizationService service, ITracingService log)
{
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
//getting target entity
Entity contact = (Entity)context.InputParameters["Target"];

//initialize email entity
Entity email = new Entity("email");

Entity fromparty = new Entity("activityparty");
Entity toparty = new Entity("activityparty");

log.Trace(context.UserId.ToString());

fromparty["partyid"] = new EntityReference("systemuser",context.UserId);
toparty["partyid"] = new EntityReference("contact", contact.Id);

email["from"] = new Entity[] { fromparty };
email["to"] = new Entity[] { toparty };
email["subject"] = "Welcome " + contact.Attributes["fullname"] + " " + DateTime.Now.ToString();
email["description"] = "Hi " + contact.Attributes["fullname"] + ",\n\tThe contact has been created successfully";
email["directioncode"] = true;
email["regardingobjectid"] = new EntityReference("contact", contact.Id);

//create an email activity record
Guid emailId = service.Create(email);

//send email to the recipient
SendEmailRequest emailRequest = new SendEmailRequest
{
EmailId = emailId,
TrackingToken = "",
IssueSend = true
};
//getting email response
SendEmailResponse emailResponse = (SendEmailResponse)service.Execute(emailRequest);
log.Trace(emailResponse.ResponseName);
}
}
catch(Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}

}

Result:

Have a great day!

Originally published at https://www.tamilarasu.me on June 25, 2023.

--

--

Tamilarasu Arunachalam
Tamilarasu Arunachalam

Written by Tamilarasu Arunachalam

A Software Engineer with experience in developing applications out of Power Platform, majorly on Power Apps and Power Automate. I am a seasonal blogger too.

No responses yet