Export (0) Print
Expand All
Expand Minimize

SendEmailFromTemplate Message (CrmService)

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Works for all deployment typesWorks online only

Sends an e-mail message using a template.

The relevant classes are specified in the following table.

TypeClass
RequestSendEmailFromTemplateRequest
ResponseSendEmailFromTemplateResponse
Entityemail

Remarks

To use this message, pass an instance of the SendEmailFromTemplateRequest class as the request parameter in the Execute method.

To perform this action, the caller must have access rights on the e-mail entity instance. For a list of required privileges, see SendEmailFromTemplate Privileges.

Example

The following code example shows how to use the SendEmailFromTemplate message.

[C#]
// Set up the CRM Service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0; 
token.OrganizationName = "AdventureWorksCycle";
 
CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Create the 'From:' activity party for the e-mail.
activityparty fromParty = new activityparty();
fromParty.partyid = new Lookup();
fromParty.partyid.type = EntityName.systemuser.ToString();
fromParty.partyid.Value = new Guid("7E91958D-C8A1-404C-AC2C-9C474FB2427B");

// Create the 'To:' activity party for the e-mail.
activityparty toParty = new activityparty();
toParty.partyid = new Lookup();
toParty.partyid.type = EntityName.contact.ToString();
toParty.partyid.Value = new Guid("44E05740-607B-47AA-ABD6-13A007E2DD85");

// Create an e-mail message.
email email = new email();

// Set e-mail properties.
email.to = new activityparty[] { toParty };
email.from = new activityparty[] { fromParty };
email.subject = "SDK Sample email";
email.description = "SDK Sample for SendEmailFromTemplate Message.";

CrmBoolean direction = new CrmBoolean();
direction.Value = true;
email.directioncode = direction;
    
TargetSendFromTemplateEmail emailTemplateTarget = new TargetSendFromTemplateEmail();
emailTemplateTarget.Email = email;

SendEmailFromTemplateRequest emailRequest = new SendEmailFromTemplateRequest();
emailRequest.Target = emailTemplateTarget;

// The regarding ID is required and must be of the same type as the e-mail template.
emailRequest.RegardingId = new Guid("105BE43F-5911-DD11-9778-0003FFBDD2C7");
emailRequest.RegardingType = EntityName.contact.ToString();

// Use a built-in email template of type "contact".
emailRequest.TemplateId = new Guid("7816B01C-EFA8-4396-8BA5-0B6B72DA5C08");

SendEmailFromTemplateResponse emailResponse = (SendEmailFromTemplateResponse)service.Execute(emailRequest);
[Visual Basic .NET]
' Set up the CRM Service.
Dim token As New CrmAuthenticationToken()
' You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0
token.OrganizationName = "AdventureWorksCycle";
 
Dim service As New CrmService()
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials

' Create a user to send an email to (To: field) 
Dim emailContact As New contact() 
emailContact.firstname = "Adam" 
emailContact.lastname = "Carter" 
emailContact.emailaddress1 = "adamc@microsoft.com" 

' Create the contact 
Dim emailContactId As Guid = service.Create(emailContact) 

' Get a system user to send the email (From: field) 
Dim systemUserRequest As New WhoAmIRequest() 
Dim systemUser As WhoAmIResponse = DirectCast(service.Execute(systemUserRequest), WhoAmIResponse) 

' Create the 'From:' activity party for the email 
Dim fromParty As New activityparty() 
fromParty.partyid = New Lookup() 
fromParty.partyid.type = EntityName.systemuser.ToString() 
fromParty.partyid.Value = systemUser.UserId 

' Create the 'To:' activity party for the email 
Dim toParty As New activityparty() 
toParty.partyid = New Lookup() 
toParty.partyid.type = EntityName.contact.ToString() 
toParty.partyid.Value = emailContactId 

' Create an email message. 
Dim email As New email() 

' Set email properties 
email.[to] = New activityparty() {toParty} 
email.from = New activityparty() {fromParty} 
email.subject = "SDK Sample email" 
email.description = "SDK Sample for SendEmailFromTemplate Message." 

Dim direction As New CrmBoolean() 
direction.Value = True 
email.directioncode = direction 

Dim emailTemplateTarget As New TargetSendFromTemplateEmail() 
emailTemplateTarget.Email = email 

Dim emailRequest As New SendEmailFromTemplateRequest() 
emailRequest.Target = emailTemplateTarget 

' The regarding Id is requried and must be of the same type as the Email Template 
emailRequest.RegardingId = new Guid("105BE43F-5911-DD11-9778-0003FFBDD2C7"); 
emailRequest.RegardingType = EntityName.contact.ToString() 

' Use a built-in email template of type "contact". 
emailRequest.TemplateId = New Guid("7816B01C-EFA8-4396-8BA5-0B6B72DA5C08") 

Dim emailResponse As SendEmailFromTemplateResponse = DirectCast(service.Execute(emailRequest), SendEmailFromTemplateResponse)

See Also

Concepts

Reference


© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2014 Microsoft