web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Create Email Activity using email template C#

(0) ShareShare
ReportReport
Posted on by 1,000


I'm using the following code for send email using email Template.

public void SendEmailUsingTemplate(IOrganizationService crmService,ActivityParty[] fromParty,  ctivityParty[] toParty,string templateName,Guid regardingId, string regardingType)

{

try

{

// Create e-mail message.

var email = new Email

{

To = toParty,

From = fromParty,

DirectionCode = true

};

if (!string.IsNullOrEmpty(templateName))

{

Guid templateId = Guid.Empty;

// Get Template Id by Name

Entity template = GetTemplateByName(crmService, templateName);  // Here I got the template Details

if (template != null && template.Id != null)

{

var emailUsingTemplateReq = new SendEmailFromTemplateRequest

{

Target = email.ToEntity<Entity>(),

TemplateId = template.Id,

RegardingId = regardingId,

RegardingType = regardingType

};

var emailUsingTemplateResp = (SendEmailFromTemplateResponse)crmService.Execute(emailUsingTemplateReq);

}

else

{

// “****No email template exists with the given name ****”);

}

}

}

catch (Exception ex)

{

throw;

}

}

 But I need just create a email with retrieved email template in c# ? How can I achieve this ?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I believe you should use InstantiateTemplateRequest/InstantiateTemplateResponse instead. Here is an example - msdn.microsoft.com/.../hh210212.aspx

  • Community Member Profile Picture
    on at

    You can find the example code on SDK -> Sample Code -> CS -> BussinessDataModel -> Templates -> "GetEmailTemplateAttachments".

    Here is the example from those sdk:

    QueryExpression query = new QueryExpression
                        {
                            EntityName = ActivityMimeAttachment.EntityLogicalName,
                            ColumnSet = new ColumnSet("filename"),
    
                            //Define the conditions for each attachment.
                            Criteria =
                            {
                                FilterOperator = LogicalOperator.And,
                                Conditions =
                            {
                                //The ObjectTypeCode must be specified, or else the query
                                //defaults to "email" instead of "template".
                                new ConditionExpression
                                {
                                    AttributeName = "objecttypecode",
                                    Operator = ConditionOperator.Equal,
                                    Values = {Template.EntityTypeCode}
                                },
                                //Specify which template we need.
                                new ConditionExpression
                                {
                                    AttributeName = "objectid",
                                    Operator = ConditionOperator.Equal,
                                    Values = {_emailTemplateId}
                                }
                            }
                            }
                        };
    
                        //Write out the filename of each attachment retrieved.
                        foreach (ActivityMimeAttachment attachment in _serviceProxy.RetrieveMultiple(query).Entities)
                        {
                            Console.WriteLine("Retrieved attachment {0}", attachment.FileName);
                        }
    
                        //</snippetGetEmailTemplateAttachments1>
    
                        DeleteRequiredRecords(promptForDelete);
                    }
                }
    
    


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans