Blog Archives

How to send an Email using Email Template from CRM 2011 using C#

Top_bottom banner
//This post demonstrates how to send an email using a template from CRM 2011 using C# code
//There are 4 steps in achieving this:
(a)      Create the ‘From:’ activity party for the email
(b)     Create the ‘To:’ activity party for the email
(c)    Create an e-mail message
(d)    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;
//(a)Create the ‘From:’ activity party for the email
ActivityParty fromParty = new ActivityParty
{
    PartyId = new EntityReference(SystemUser.EntityLogicalName, _userId)
};
//(b)Create the ‘To:’ activity party for the email
ActivityParty toParty = new ActivityParty
{
    PartyId = new EntityReference(Contact.EntityLogicalName, _contactId)
};
//(c)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
};

 

// (d)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;
//#########################################################################
//Here is the 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(“<Contact Record GUID>”);
    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(“<Template GUID>”),
        // 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;
}
Please share |Like in Facebook if you feel like this page is useful.
Top_bottom banner

How to send an Email from CRM 2011 using C#

This post demonstrates how to send an email from CRM 2011 using C# code.

There are 4 steps in achieving this:

(a)    Create the ‘From:’ activity party for the email

(b)    Create the ‘To:’ activity party for the email

(c)    Create an e-mail message

(d)    Create the request to send email

 

//Namespaces 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;

 

//(a) Create the ‘From:’ activity party for the email

ActivityParty fromParty = new ActivityParty

{

PartyId = new EntityReference(SystemUser.EntityLogicalName, _userId)

};

 

//(b) Create the ‘To:’ activity party for the email

ActivityParty toParty = new ActivityParty

{

PartyId = new EntityReference(Contact.EntityLogicalName, _contactId)

};

 

//(c) Create an e-mail message

Email email = new Email

{

To = new ActivityParty[] { toParty },

From = new ActivityParty[] { fromParty },

Subject = “e-mail”,

Description = “SendEmail Message.”,

DirectionCode = true

};

 

//(d) Create the request to send email

SendEmailRequest sendEmailreq = new SendEmailRequest

{

EmailId = _emailId,

TrackingToken = “”,

IssueSend = true

};

 

SendEmailResponse sendEmailresp = (SendEmailResponse)_serviceProxy.Execute(sendEmailreq);

 

//#########################################################################################

Top_bottom banner

//Here is the 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(“<Contact Record ID>”);

 

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 = “SendEmail Message.”,

DirectionCode = true

};

Guid _emailId = _serviceProxy.Create(email);

 

// Use the SendEmail message to send an e-mail message.

SendEmailRequest sendEmailreq = new SendEmailRequest

{

EmailId = _emailId,

TrackingToken = “”,

IssueSend = true

};

 

SendEmailResponse sendEmailresp = (SendEmailResponse)_serviceProxy.Execute(sendEmailreq);

}

Please share |Like in Facebook if you feel like this page is useful.

Top_bottom banner

How to send HTML content in email in CRM 2011

This post covers how to send html content in an email from CRM 2011.

Steps to follow:

1. Create a .html page with the html content that you need to include in your email and open it in a browser
2. Create a new email template in CRM 2011 and copy paste the html content from the browser to the message area in the Email Template

3. Save the Email Template

4. Create a new email and insert the Email Template which you just created with the required HTML content. When you write the C# code to create the email, just use the same Email template.