In this blog, I will show you how to send email using a plugin in Dynamics 365 using C#.
For this example, let us suppose we need to send an email to the new contact that has been created.
First, create a new class library project in Visual Studio, as shown in the image below.
Now create a class named SendEmail.cs and add the following references to your project using NuGet and then add it to the top of the class.
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
Now add the following code in the file.
namespace SendEmailPlugin
{
public class SendEmail : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Step - 1
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService Service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
if(context.InputParameters.Contains("Target") && context.InputParameters is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters;
if (entity.Attributes.Contains("emailaddress1") && entity.Attributes.ToString() != "")
{
// Step - 2
// Creating the 'From' activity party for the email
Entity fromParty = new Entity("activityparty");
fromParty = new EntityReference("systemuser", context.UserId);
Entity toParty = new Entity("activityparty");
toParty = new EntityReference("contact", entity.Id);
// Step - 3
Entity email = new Entity("email");
email = new Entity[] { fromParty };
email = new Entity[] { toParty };
email = "Welcome to Contoso Inc.";
email = "<p>You have been added as a Contact. You are now eligible to redeem our special offers.</p>"; // You can write html emails
email = new EntityReference("contact", entity.Id);
email = true;
Guid emailGuid = Service.Create(email);
// Step - 4
SendEmailRequest sendEmailRequest = new SendEmailRequest
{
EmailId = emailGuid,
TrackingToken = "",
IssueSend = true
};
SendEmailResponse sendEmailresp = (SendEmailResponse)Service.Execute(sendEmailRequest);
tracingService.Trace("Email Sent"); }
}
}
catch(Exception ex)
{
throw new InvalidPluginExecutionException("An error occured while running plugin.", ex);
}
}
}
}
Below is the explanation of the above code -
Step 1:
Firstly, we will extract the context and other important variables from the PluginContext.
Step 2:
In this step we are setting to and from attributes for the email. The to and from attributes are basically the instance of ActivityParty entity in CRM, in which we have to set the partyid attribute which is of type EntityReference. In this example we are setting the fromParty to the user who has called the plugin.
Step 3:
In this step we are creating a record in email entity. In the description attribute you can also add html. The create method returns the GUID of newly created record which we will use to create a send email request.
Step 4:
Using step 3 we are creating an email, but it will not send it. To send an email we have to create a SendEmailRequest using the CRM’s inbuilt class which takes email GUID as a parameter. We are using Execute method to execute the request.
Add this plugin on create of Contact entity and if all goes well, you can see the newly created email using advanced find tool in CRM.
If your email settings are correctly configured in the CRM, it will send the email to the user and set the Status Reason to Sent.