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);
}