Plugin's are very useful business process components within CRM, they can be attached and configured to execute based on certain events and execute pre-defined business logic.
How to send an Email from a plugin is today's post, code example:
The method demonstrates how to send an email, passing in the appropriate parameters. This example is sending an email from and to internal System Users, however this can be easily adapted for other purposes.
Note: Your email mechanism need's to be configured correctly in order for this to work e.g. CRM Email Router.
How to send an Email from a plugin is today's post, code example:
private void SendEmail(IOrganizationService service,
Guid recieverUserId,
Guid senderUserId,
Guid regardingObjectId,
string emailBody,
string emailSubject)
{
Entity email = new Entity();
email.LogicalName = "email";
//Regarding field, the record, which you want this email associated to
EntityReference regardingObject = new EntityReference("opportunity", regardingObjectId);
email.Attributes.Add("regardingobjectid",regardingObject);
//Define the Sender and Receiver
EntityReference from = new EntityReference("systemuser", senderUserId);
EntityReference to = new EntityReference("systemuser", recieverUserId);
//Activity Party - From
Entity fromParty = new Entity("activityparty");
fromParty.Attributes.Add("partyid",from);
//Activity Party - To
Entity toParty = new Entity("activityparty");
toParty.Attributes.Add("partyid", to);
EntityCollection collFromParty = new EntityCollection();
collFromParty.EntityName = "systemuser";
collFromParty.Entities.Add(fromParty);
EntityCollection collToParty = new EntityCollection();
collToParty.EntityName = "systemuser";
collToParty.Entities.Add(toParty);
email.Attributes.Add("from",collFromParty);
email.Attributes.Add("to", collToParty);
//Set Email Subject & Body
email.Attributes.Add("subject",emailSubject);
email.Attributes.Add("description", emailBody);
//Create email activity
Guid emailID = service.Create(email);
//Sending email
SendEmailRequest reqSendEmail = new SendEmailRequest();
reqSendEmail.EmailId = emailID;
reqSendEmail.TrackingToken = string.Empty;
reqSendEmail.IssueSend = true;
SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);
}
Guid recieverUserId,
Guid senderUserId,
Guid regardingObjectId,
string emailBody,
string emailSubject)
{
Entity email = new Entity();
email.LogicalName = "email";
//Regarding field, the record, which you want this email associated to
EntityReference regardingObject = new EntityReference("opportunity", regardingObjectId);
email.Attributes.Add("regardingobjectid",regardingObject);
//Define the Sender and Receiver
EntityReference from = new EntityReference("systemuser", senderUserId);
EntityReference to = new EntityReference("systemuser", recieverUserId);
//Activity Party - From
Entity fromParty = new Entity("activityparty");
fromParty.Attributes.Add("partyid",from);
//Activity Party - To
Entity toParty = new Entity("activityparty");
toParty.Attributes.Add("partyid", to);
EntityCollection collFromParty = new EntityCollection();
collFromParty.EntityName = "systemuser";
collFromParty.Entities.Add(fromParty);
EntityCollection collToParty = new EntityCollection();
collToParty.EntityName = "systemuser";
collToParty.Entities.Add(toParty);
email.Attributes.Add("from",collFromParty);
email.Attributes.Add("to", collToParty);
//Set Email Subject & Body
email.Attributes.Add("subject",emailSubject);
email.Attributes.Add("description", emailBody);
//Create email activity
Guid emailID = service.Create(email);
//Sending email
SendEmailRequest reqSendEmail = new SendEmailRequest();
reqSendEmail.EmailId = emailID;
reqSendEmail.TrackingToken = string.Empty;
reqSendEmail.IssueSend = true;
SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);
}
The method demonstrates how to send an email, passing in the appropriate parameters. This example is sending an email from and to internal System Users, however this can be easily adapted for other purposes.
Note: Your email mechanism need's to be configured correctly in order for this to work e.g. CRM Email Router.
No comments:
Post a Comment