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

Advertisements

About Dynamics CRM Guru

We are a group of CRM Consultants and have significant experience in transforming businesses in the right way. We use this platform to share our knowledge which might help many others in the digital transformation journey. We are happy to help you if you are looking for any assistance in CRM implementation projects.

Posted on March 12, 2013, in Microsoft CRM 2011, MS CRM 2011 Training and tagged . Bookmark the permalink. 1 Comment.

  1. World Traveler

    MailMessage mail = new MailMessage();

    string to = txtMailTo.Text;
    string from = txtMailFrom.Text;
    string subject = txtMailSub.Text;
    string body = txtMailBody.Text;

    MailMessage message = new MailMessage(from, to, subject, body);
    message.IsBodyHtml = true;

    SmtpClient client = new SmtpClient(Host, Port);
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Send(message);

    a good reference with example : http://cybarlab.blogspot.com/2013/03/send-e-mail-using-c-sharp.html

    Liked by 1 person

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

%d bloggers like this: