/** Send an E-Mail on Account Create using PLUGIN **/
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;
using Microsoft.Xrm.Sdk.Messages;
using System.Runtime.Serialization;
using System.ServiceModel;
namespace Email.Account.Create
{
public class mailtosend : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService( typeof( IPluginExecutionContext));
if (context.InputParameters. Contains("Target") && context.InputParameters[" Target"] is Entity)
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory) serviceProvider.GetService( typeof( IOrganizationServiceFactory));
IOrganizationService service = serviceFactory. CreateOrganizationService( context.UserId);
Entity account = (Entity)context. InputParameters["Target"];
Entity email = new Entity("email");
Entity fromParty = new Entity("activityparty");
Entity toParty = new Entity("activityparty");
toParty["partyid"] = new EntityReference("systemuser", context.UserId);
fromParty["partyid"] = new EntityReference("systemuser", context.UserId);
email["from"] = new Entity[] { fromParty };
email["to"] = new Entity[] { toParty };
email["subject"] = "email subject - " + DateTime.Now.ToString();
email["description"] = "email description";
email["regardingobjectid"] = new EntityReference("account", account.Id);
Guid emailId = service.Create(email);
SendEmailRequest sendEmailreq = new SendEmailRequest
{
EmailId = emailId,
TrackingToken = "",
IssueSend = true
};
SendEmailResponse sendEmailresp = (SendEmailResponse)service. Execute(sendEmailreq);
}
}
}
}