Send Birthday Email To Customers [ Simplest Code Solution]


This is a very common and simple requirement you will get in CRM wherein you have to find a way to send email to customers whose birthday is today. whilst you can achieve this functionality without any code ( workflows + custom fields/entities) but today i will be providing/showing a sample code on how to do that.

This is a simple console application which you can schedule to run every day at specific time.


  1. LinqKit ( you can download from Nuget Manager & add under references)
  2. Early Bound Class



Main Program.cs :

using System;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using System.Net;
using System.Configuration;
using Microsoft.Xrm.Sdk.Client;
using EarlyBound;
using LinqKit;

namespace BirthdayEmail
 class Program
 static void Main(string[] args)
 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
 CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString);

IOrganizationService crmService = crmConn.OrganizationServiceProxy;

OrganizationServiceContext orgContext = new OrganizationServiceContext(crmService);

//use linqkit to build predicate
var predicate = PredicateBuilder.False<Contact>();
 for (int i = Math.Min(140, DateTime.Today.Year - 1900); i > -1; i--)
 DateTime cleanDateTime = new DateTime(DateTime.Today.AddYears(-i).Year, DateTime.Today.AddYears(-1).Month, DateTime.Today.AddYears(-i).Day);
 predicate = predicate.Or(p => p.BirthDate == cleanDateTime.ToUniversalTime());
 var getBirthdays = (from c in orgContext.CreateQuery<Contact>().AsExpandable().Where(predicate)
 select c).ToList();

foreach (var ent in getBirthdays)
 Guid _contactid = ent.Id;

Helper helper = new Helper();
 helper.CreateRequiredRecords(crmService, _contactid);



using System;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using EarlyBound;

namespace BirthdayEmail
 class Helper
 public void CreateRequiredRecords(IOrganizationService crmService, Guid _contactid)

WhoAmIRequest systemUserRequest = new WhoAmIRequest();
 WhoAmIResponse systemUserResponse = (WhoAmIResponse)crmService.Execute(systemUserRequest);
 var userId = systemUserResponse.UserId;

// Create 'From' activity party for the email

ActivityParty fromParty = new ActivityParty
 PartyId = new EntityReference(SystemUser.EntityLogicalName,userId)

// Create 'To' activity party for the email

ActivityParty toParty = new ActivityParty
 PartyId = new EntityReference(Contact.EntityLogicalName, _contactid)

 //format the description field - optional

StringBuilder description = new StringBuilder();
 description.Append("Dear Customer <br/> <br/>");
 description.Append("Wish you a very happy birthday and great life ahead. <br/> <br/>");
 description.Append("Regards, <br/>");

// Create an e-mail message
 Email email = new Email
 To = new ActivityParty[] { toParty },
 From = new ActivityParty[] { fromParty },
 Subject = "Happy Birthday!",
 Description = description.ToString(),
 DirectionCode = true,

Guid _emailId = crmService.Create(email);
 //Send Email

SendEmailRequest sendEmail = new SendEmailRequest
 EmailId = _emailId,
 TrackingToken = "",
 IssueSend = true

SendEmailResponse response = (SendEmailResponse)crmService.Execute(sendEmail);

if (response != null)



<?xml version="1.0" encoding="utf-8"?>
 <!-- For more information on Entity Framework configuration, visit -->
 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 <add name="CRM" connectionString="AuthType=Office365; Username=; Password=password;Url=" />
 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />

Notice i am using namescpae “EarlyBound”  – that actually my namespace of my early bound generated class.

if you need help in generating the early bound code – please follow my blog :



Few things :

  1. you can use an email template in the email request
  2. All necessary log can be generated or logged to console.

I hope this helps!



4 comments on “Send Birthday Email To Customers [ Simplest Code Solution]”

  1. Hello, I see you have blogged 2 approaches for very same business requirement, and you have mentioned here that “whilst you can achieve this functionality without any code ( workflows + custom fields/entities)”, I am very curious to know how and if you have already blogged about it please share the link or if you know there is a blog out there please share that too. Thank You.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.