Michael Philip's Blog

Writing about MS Tech with strong focus on Azure & CRM

MS CRM 2011 – Send an Email Using a Template programatically

Leave a comment

Code:

// Create the ‘From:’ activity party for the email
ActivityParty fromParty = new ActivityParty
{
PartyId = new EntityReference(SystemUser.EntityLogicalName, _userId)
};

// Create the ‘To:’ activity party for the email
ActivityParty toParty = new ActivityParty
{
PartyId = new EntityReference(Contact.EntityLogicalName, _contactId)
};

// Create an e-mail message.
Email email = new Email
{
To = new ActivityParty[] { toParty },
From = new ActivityParty[] { fromParty },
Subject = “SDK Sample e-mail”,
Description = “SDK Sample for SendEmailFromTemplate Message.”,
DirectionCode = true
};

// Create the request
SendEmailFromTemplateRequest emailUsingTemplateReq = new SendEmailFromTemplateRequest
{
Target = email,

// Use a built-in Email Template of type “contact”.
TemplateId = new Guid(“07B94C1D-C85F-492F-B120-F0A743C540E6”),

// The regarding Id is required, and must be of the same type as the Email Template.
RegardingId = _contactId,
RegardingType = Contact.EntityLogicalName
};

SendEmailFromTemplateResponse emailUsingTemplateResp = (SendEmailFromTemplateResponse)_serviceProxy.Execute(emailUsingTemplateReq);

 

Advertisements

Leave a Reply

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