Compartir a través de


SendEmailFromTemplateRequest Class

Definition

Contains the data that is needed to send an email message using a template.

public ref class SendEmailFromTemplateRequest sealed : Microsoft::Xrm::Sdk::OrganizationRequest
[System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/crm/2011/Contracts")]
public sealed class SendEmailFromTemplateRequest : Microsoft.Xrm.Sdk.OrganizationRequest
[<System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/crm/2011/Contracts")>]
type SendEmailFromTemplateRequest = class
    inherit OrganizationRequest
Public NotInheritable Class SendEmailFromTemplateRequest
Inherits OrganizationRequest
Inheritance
SendEmailFromTemplateRequest
Attributes

Examples

The following example shows how to use this message. For this sample to work correctly, you must have an authenticated connection to the server with a client that implements the IOrganizationService interface instance.

/// <summary>
/// Sends an email from a template
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="contactId">The id of a contact record which has a valid email address.</param>
/// <exception cref="ArgumentException"></exception>
static void SendEmailFromTemplateRequestSample(IOrganizationService service, Guid contactId)
{
    Guid _templateId;
    var whoAmIResponse = (WhoAmIResponse)service.Execute(new WhoAmIRequest());

    // Create the 'From:' activity party for the email
    var fromParty = new Entity("activityparty")
    {
        Attributes =
        {
            { "partyid", new EntityReference("systemuser", whoAmIResponse.UserId)}
        }
    };

    // Create the 'To:' activity party for the email
    var toParty = new Entity("activityparty")
    {
        Attributes =
        {
            { "partyid", new EntityReference("contact", contactId)}
        }
    };

    var email = new Entity("email")
    {
        Attributes = {
            { "to", new Entity[] { toParty } },
            { "from", new Entity[] { fromParty } },
            { "subject", "SDK Sample email" },
            { "description","SDK Sample for SendEmailFromTemplate Message." },
            { "directioncode", true }
        }
    };


    //Create a query expression to get one of Email Template of type "contact"

    QueryExpression queryBuildInTemplates = new QueryExpression
    {
        EntityName = "template",
        ColumnSet = new ColumnSet("templateid", "templatetypecode"),
        Criteria = new FilterExpression()
    };
    queryBuildInTemplates.Criteria.AddCondition("templatetypecode",
        ConditionOperator.Equal, "contact");
    EntityCollection templateEntityCollection = service.RetrieveMultiple(queryBuildInTemplates);

    if (templateEntityCollection.Entities.Count > 0)
    {
        _templateId = (Guid)templateEntityCollection.Entities[0].Attributes["templateid"];
    }
    else
    {
        throw new ArgumentException("Standard Email Templates are missing");
    }

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

        // Use a built-in Email Template of type "contact".
        TemplateId = _templateId,

        // The regarding Id is required, and must be of the same type as the Email Template.
        RegardingId = contactId,
        RegardingType = "contact"
    };

    var response = (SendEmailFromTemplateResponse)service.Execute(request);


    // Verify that the e-mail has been created
    if (!response.Id.Equals(Guid.Empty))
    {
        Console.WriteLine("Successfully sent an e-mail message using the template.");
    }
}

Remarks

For the Web API use the SendEmailFromTemplate action.

Usage

Pass an instance of this class to the Execute(OrganizationRequest) method, which returns an instance of SendEmailFromTemplateResponse.

Privileges and Access Rights

To perform this action, the caller must have privileges on the Email entity and access rights on the records specified in the Target, RegardingId, and TemplateId properties.

Constructors

SendEmailFromTemplateRequest()

Initializes a new instance of theSendEmailFromTemplateRequest class.

Properties

ExtensionData

Gets or sets the structure that contains extra data. Optional.

(Inherited from OrganizationRequest)
Item[String]

Gets or sets the indexer for the Parameters collection.

(Inherited from OrganizationRequest)
Parameters

Gets or sets the collection of parameters for the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)
RegardingId

Gets or sets the ID of the record with which the email message is associated.

RegardingType

Gets or sets the type of the record with which the email message is associated.

RequestId

Gets or sets the ID of the request. Optional.

(Inherited from OrganizationRequest)
RequestName

Gets or sets the name of the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)
Target

Gets or sets the email record to send.

TemplateId

Gets or sets the ID of the email template to use for the email.

Applies to

See also