CRM 2011 CS: Send an Email in CRM 2011

// Send an Email in CRM 2011

 

 

// Namespaces

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;

 

 

 

//CODE:Send an Email in CRM 2011

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 = "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);

}