Pages

Tuesday, 7 August 2012

Send an Email Using a Template in CRM 2011

In this article , I am going to explain how to send an email using template id

For Better Understanding I divided this article in four parts
(i)      Create the 'From:' activity party for the email
(ii)     Create the 'To:' activity party for the email
(iii)    Create an e-mail message
(iv)    Create the request to send email

Namespace need to include 
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;

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 = "e-mail",
    Description = "SendEmailFromTemplate Message.",
    DirectionCode = true
};

Create the request to send email
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);
Guid _emailId = emailUsingTemplateResp.Id;

Complete Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
                                                    serverConfig.HomeRealmUri,
                                                    serverConfig.Credentials,
                                                    serverConfig.DeviceCredentials))
{
    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

    Guid _contactId = new Guid("67B45Cdf-C56F-456F-B145-1237435430E6");

    WhoAmIRequest systemUserRequest = new WhoAmIRequest();
    WhoAmIResponse systemUserResponse = (WhoAmIResponse)_serviceProxy.Execute(systemUserRequest);
    Guid _userId = systemUserResponse.UserId;

    // 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 = "e-mail",
        Description = "SendEmailFromTemplate Message.",
        DirectionCode = true
    };

    // Create the request to send email
    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);
    Guid _emailId = emailUsingTemplateResp.Id;
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.