Send Email using Template

public void SendMail(ICrmService service,Guid ownerID, Guid fromGuid, ArrayList toGuid,Guid recordRegardingID,Guid templateId,Enum  enitityName)
{

// Create an instance of email
email myEmail = new email();
//set the owner of the mail
myEmail.ownerid = new Owner();
myEmail.ownerid.type = EntityName.systemuser.ToString();
myEmail.ownerid.Value = ownerID;
// specify the from part of the email
activityparty from = new activityparty();
from.partyid = new Lookup();
from.partyid.type = EntityName.systemuser.ToString();
// guid of the system user who is sending the mail
from.partyid.Value = fromGuid;
myEmail.from = new activityparty[] { from };

activityparty[] apTo = new activityparty[toGuid.Count];
// creating as many activity party as the no of users or members in a team
int i = 0;
foreach (String memberID in toGuid)
{
apTo[i] = new activityparty();
apTo[i].partyid = new Lookup();
apTo[i].partyid.type = EntityName.systemuser.ToString();
apTo[i].partyid.Value = new Guid(memberID);
i++;
}

myEmail.to = apTo;
TargetSendFromTemplateEmail myTargetSendFromTemplateEmail = new TargetSendFromTemplateEmail();
myTargetSendFromTemplateEmail.Email = myEmail;
SendEmailFromTemplateRequest mySendEmailFromTmpRequest = new SendEmailFromTemplateRequest();
// Specify entity instance id for RegardingID
mySendEmailFromTmpRequest.RegardingId = recordRegardingID;
// Specify the type of entity
mySendEmailFromTmpRequest.RegardingType = enitityName.ToString();
// Specify the email instance to be sent
mySendEmailFromTmpRequest.Target = myTargetSendFromTemplateEmail;
// Specify the template to be used ( Either a global or created specfially for that entity)
mySendEmailFromTmpRequest.TemplateId = templateId;
SendEmailFromTemplateResponse mySendEmailFromTmpResponse =(SendEmailFromTemplateResponse)service.Execute(mySendEmailFromTmpRequest);

}

Author: Arvind Singh

Solution Architect with 17+ years of exp. In Dynamics CRM, Power Platform, Azure which includes Solution, Design, Development, Deployment, Maintenance and support experience.

4 thoughts on “Send Email using Template”

  1. Hi,
    Is there any way to set the mySendEmailFromTmpRequest.RegardingType to a custom entity like mySendEmailFromTmpRequest.RegardingType = “new_provider”? This should be possible because we can change the regarding on an email that’s already sent to may be a contact?

    Thanks,
    Murthy

    Like

    1. Hi Murthy,

      Sorry for the deley.
      The regarding ID is required and must be of the same type as the e-mail template Since we can’t create e-mail template for custom entity. So I think it’s not possible to set RegardingType= “new_provider”(Custom entity).

      Regards,
      Arvind

      Like

      1. Arvind,
        It doesn’t make sense because we can change the regardingtype to a custom entity once the email is sent.

        Can you check further?

        Like

Leave a comment