Send Email in CRM using a Template (SendEmailFromTemplateRequest)

Hi using the following code we can send an email programmatically that uses the email-template withing CRM

Suppose you have created a web application and have a button named btnSendEmail over there.

Put the following code in it’s event handler

protected void btnSendEmail_Click(object sender, EventArgs e)

{

CrmAuthenticationToken token = new CrmAuthenticationToken();

token.AuthenticationType = 0; // AD ( On-Premise)

token.OrganizationName = “nameofyourorganization”;

CrmService myService = new CrmService();

myService.Credentials = System.Net.CredentialCache.DefaultCredentials;

myService.CrmAuthenticationTokenValue = token;

// 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 = new Guid(“26063267-7B85-DD11-B2E0-00164145E126”);

// 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 = new Guid(“24543267-7B85-DD11-B2E0-00164145E126”);

myEmail.from = new activityparty[] { from };

// specify the to part of the email

activityparty to = new activityparty();

to.partyid = new Lookup();

to.partyid.type = EntityName.systemuser.ToString();

to.partyid.Value = new Guid(“26063267-7B85-DD11-B2E0-00164145E126”);

myEmail.to = new activityparty[] { to };

TargetSendFromTemplateEmail myTargetSendFromTemplateEmail = new TargetSendFromTemplateEmail();

myTargetSendFromTemplateEmail.Email = myEmail;

SendEmailFromTemplateRequest mySendEmailFromTmpRequest = new SendEmailFromTemplateRequest();

// Specify entity instance id for RegardingID

mySendEmailFromTmpRequest.RegardingId = new Guid(“132C847E-028A-DD11-8E7E-00164145E126”);

// Specify the type of entity

mySendEmailFromTmpRequest.RegardingType = EntityName.opportunity.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 = new Guid(“6963788F-048A-DD11-8E7E-00164145E126”);

SendEmailFromTemplateResponse mySendEmailFromTmpResponse =(SendEmailFromTemplateResponse)                                  myService.Execute(mySendEmailFromTmpRequest);

}

Bye….


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

5 thoughts on “Send Email in CRM using a Template (SendEmailFromTemplateRequest)”

  1. SendEmailFromTemplateRequest does not work with on premise crm 2011.
    IS there any other way or any other message class to send mail using template?

    Like

  2. Thanks for another informative site. Where else could I get that type of information written in such an ideal way? I’ve a project that I am just now working on, and I have been on the look out for such information.

    Like

  3. We use custom code to send mails from CRM environment.

    TargetSendFromTemplateEmail myTargetSendFromTemplateEmail = new TargetSendFromTemplateEmail();

    myTargetSendFromTemplateEmail.Email = myEmail;

    SendEmailFromTemplateRequest mySendEmailFromTmpRequest = new SendEmailFromTemplateRequest();

    mySendEmailFromTmpRequest.Target = myTargetSendFromTemplateEmail;

    mySendEmailFromTmpRequest.RegardingId = guidRegardingObjectid;

    mySendEmailFromTmpRequest.RegardingType = “new_course”;

    — IMPORTANT — mySendEmailFromTmpRequest.TemplateId = guidMailMergeid;

    — IMPORTANT —

    but mySendEmailFromTmpRequest.TemplateId just accepts mail tamplate and not mail merge template.

    any idea how to accomplish it on this or another way ?

    Like

Share your thoughts

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

WordPress.com Logo

You are commenting using your WordPress.com 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