Send Email using Template

Posted: July 28, 2010 in Microsoft .NET, Microsoft Dynamics CRM
Tags: , ,

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


  1. Murthy says:

    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?


    • Arvind Singh says:

      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).


      • Murthy Puvvada says:

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

        Can you check further?

  2. HI, I want to send email with email template. the sender will be static as, it will not be entity simple giving as
    and to address can be any mail like

    Please any one can help me……

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s