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