Create Email using CRM SDK and C#

Follow the below steps to create a simple Email record in Console application,

Step 1: Open Visual Studio and Create Console Application Project.

Step 2: First we have to establish connection to CRM. Please follow my earlier post Connect to CRM Online or On-premise using C# SDK.

Step 3: Generate Early bound class using Crmsvcutil.exe. Please follow my earlier post Generate Early bound Class using CrmSvcUtil.Exe in CRM.

Step 4: Add EarlyBound.cs to the project.

Step 5: Include the namespace using EarlyBound.cs.

Step 6: Below method will create Email record,

Create Email - Parameter Description

public static void SendEmail(Guid From, Guid To, string ToEntitySchemaName, string Subject, string Description)
{
try
{
// Create 'From' activity party for the email

ActivityParty fromParty = new ActivityParty
{
PartyId = new EntityReference(SystemUser.EntityLogicalName, From)
};

// Create 'To' activity party for the email

ActivityParty toParty = new ActivityParty
{
PartyId = new EntityReference(ToEntitySchemaName, To)
};

// Create an e-mail message

Email email = new Email
{
To = new ActivityParty[] { toParty },
From = new ActivityParty[] { fromParty },
Subject = Subject,
Description = Description,
DirectionCode = true
};

Guid _emailId = _service.Create(email);

SendEmailRequest sendEmailreq = new SendEmailRequest
{
EmailId = _emailId,
TrackingToken = "",
IssueSend = true
};

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

if (sendEmailresp != null)
{
Console.WriteLine("Email record created successfully");
Console.ReadKey();
}
}
catch(Exception ex)
{
Console.Write("Error " + ex.Message);
Console.ReadKey();
}
}

Step 7: Final Code will be like below,

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using EarlyBound;

namespace LinqToCRM
{
class Program
{
static IOrganizationService _service;
static void Main(string[] args)
{
ConnectToMSCRM("arunpotti@XYZOrg.onmicrosoft.com", "password", "https://XYZOrg.api.crm5.dynamics.com/XRMServices/2011/Organization.svc");

Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;

if (userid == Guid.Empty) return;

Contact c = new Contact() //Create Contact Record
{
FirstName = "Arvind Kumar",
LastName = "Shah",
MobilePhone = "9876543210",
EMailAddress1 = "arvindshah@crm2015trial.com"
};

Guid contactGuid = _service.Create(c);

if (contactGuid != Guid.Empty)
{
SendEmail(userid, contactGuid, "contact", "Test Subject", "Test Description");
}
}

public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
{
try
{
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = UserName;
credentials.UserName.Password = Password;
Uri serviceUri = new Uri(SoapOrgServiceUri);
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
proxy.EnableProxyTypes();
_service = (IOrganizationService)proxy;
}
catch (Exception ex)
{
Console.WriteLine("Error while connecting to CRM " + ex.Message);
Console.ReadKey();
}
}

public static void SendEmail(Guid From, Guid To, string ToEntitySchemaName, string Subject, string Description)
{
try
{
// Create 'From' activity party for the email

ActivityParty fromParty = new ActivityParty
{
PartyId = new EntityReference(SystemUser.EntityLogicalName, From)
};

// Create 'To' activity party for the email

ActivityParty toParty = new ActivityParty
{
PartyId = new EntityReference(ToEntitySchemaName, To)
};

// Create an e-mail message

Email email = new Email
{
To = new ActivityParty[] { toParty },
From = new ActivityParty[] { fromParty },
Subject = Subject,
Description = Description,
DirectionCode = true
};

Guid _emailId = _service.Create(email);

SendEmailRequest sendEmailreq = new SendEmailRequest
{
EmailId = _emailId,
TrackingToken = "",
IssueSend = true
};

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

if (sendEmailresp != null)
{
Console.WriteLine("Email record created successfully");
Console.ReadKey();
}
}

catch(Exception ex)
{
Console.Write("Error " + ex.Message);
Console.ReadKey();
}
}
}
}

Step 8: Execute the project for the below output, Open CRM and check for the email record

Create Email - Output

Please provide your valuable comments on this article.


Discover more from Arun Potti's Power Platform blog

Subscribe to get the latest posts to your email.

One thought on “Create Email using CRM SDK and C#

Leave a Reply