Hi,
At times we would like to send email using SendEmailRequest but still want to make use of template and replace the content in it.
For e.g. this is our sample template (Global template)

Here we would replace the [subject] and [url] placeholders.
Sample Code used
private void SendMail(object sender, EventArgs e)
{
IOrganizationService orgService = GetOrganizationService();
Entity entity = GetGlobalTemplate("My Test Template", GetOrganizationService());
Entity email = new Entity();
email.LogicalName = "email";
// get the Subject content from template and replace it with "My New Subject"
email.Attributes["subject"] = GetDataFromXml(entity.Attributes["subject"].ToString(), "match");
email.Attributes["subject"] = email.Attributes["subject"].ToString().Replace("[subject]", "My New Subject");
// get the description from template and replace [url] placeholder with a bing's url
email.Attributes["description"] = GetDataFromXml(entity.Attributes["body"].ToString(), "match");
string urlToReplace = "<a href='http://www.bing.com'>Open Bing</a>";
email.Attributes["description"] = email.Attributes["description"].ToString().Replace("[url]", urlToReplace);
List<Entity> fromtoEntities = new List<Entity>();
Entity activityParty = new Entity();
activityParty.LogicalName = "activityparty";
activityParty.Attributes["partyid"] = new EntityReference("systemuser", new Guid("FC480E73-77A9-E111-B151-00155D882D40"));
fromtoEntities.Add(activityParty);
email.Attributes["from"] = fromtoEntities.ToArray();
email.Attributes["to"] = fromtoEntities.ToArray();
email.Attributes["regardingobjectid"] = new EntityReference("contact", new Guid("2CCB57AA-A9B3-E111-B151-00155D882D40"));
Guid emailCreated = orgService.Create(email);
SendEmailRequest req = new SendEmailRequest();
req.EmailId = emailCreated;
req.TrackingToken = "";
req.IssueSend = true;
SendEmailResponse res = (SendEmailResponse)orgService.Execute(req);
}
private static string GetDataFromXml(string value, string attributeName)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
XDocument document = XDocument.Parse(value);
// get the Element with the attribute name specified
XElement element = document.Descendants().Where(ele => ele.Attributes().Any(attr => attr.Name == attributeName)).FirstOrDefault();
return element == null ? string.Empty : element.Value;
}
public static Entity GetGlobalTemplate(string title, IOrganizationService orgService)
{
Entity emailTemplate = null;
try
{
QueryExpression query = new QueryExpression();
// Setup the query for the template entity
query.EntityName = "template";
// Return all columns
query.ColumnSet.AllColumns = true;
query.Criteria = new FilterExpression();
query.Criteria.FilterOperator = LogicalOperator.And;
// Create the title condition
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "title";
condition1.Operator = ConditionOperator.Equal;
condition1.Values.Add(title);
query.Criteria.Conditions.Add(condition1);
// Execute the query and return the result
EntityCollection entityColl = orgService.RetrieveMultiple(query);
if (entityColl.Entities.Count > 0)
{
emailTemplate = entityColl.Entities[0];
}
}
catch
{
throw;
}
return emailTemplate;
}
The email activity created :-

Bye.


How to Pass multiple Contacts or users in To, CC List in Email
Hi Madhu,
List fromtoEntities = new List();
Entity activityParty = new Entity();
activityParty.LogicalName = “activityparty”;
activityParty.Attributes["partyid"] = new EntityReference(“systemuser”, new Guid(“FC480E73-77A9-E111-B151-00155D882D40″));
fromtoEntities.Add(activityParty);
Here in the above code create as many activityParty records and add them to fromtoEntities list as to whom you want to send the mail.
Nice article…