Sam's Microsoft Dynamics 365 Blog

Monday 17 September 2018

Send Email on birthday in MS Dynamics CRM Using Azure Function App


Create an Azure Function App as explained in below steps:-

Open Visual Studio 2017, Create a new Project then Select Cloud tab and then select Azure Functions.
New Project => Cloud => Azure Functions
Name it and then click Ok.



Now select:-

a.       .Net Framework
b.      Http trigger
c.       Storage Account(AzureWebJobsStorage) :  Storage Emulator
d.      Schedule: 0 */5 * * * *


After click on Ok it will create a code. In this code it will execute every day at 8:30 AM, and print a log.  Code will look like as shown in below screenshot:


Now Run it locally. Then a Console window will open Also opens a Windows security Alert then Allow Access , looks like as shown in below screenshot:-



It will execute. Also we can debug it.


Now we can check log inside console, As shown in below screenshot.



Now we are going to put below code of sending emails, also putting TimerTrigger as 0 30 8 * * * (it will execute every day at 8: 30 AM)

Main Run method:-
var connectionString = ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString; ;
            // Connect to the CRM web service using a connection string.
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //need to add ref System.Net
            CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString);
            // Cast the proxy client to the IOrganizationService interface.
            IOrganizationService _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

            var query = new QueryExpression("contact");

            ConditionExpression condition1 = new ConditionExpression();
            condition1.AttributeName = "birthdate";
            condition1.Operator = ConditionOperator.Equal;
            condition1.Values.Add(DateTime.Now);

            FilterExpression contactfilter = new FilterExpression();
            contactfilter.Conditions.Add(condition1);

           query.ColumnSet = new ColumnSet(true);
            query.Criteria.AddFilter(contactfilter);

            EntityCollection TodayBirthdayReq = _orgService.RetrieveMultiple(query);
            foreach (Entity contact in TodayBirthdayReq.Entities)
            {
                if (contact.Attributes.Contains("emailaddress1") && contact.Attributes.Contains("fullname"))
                {
                    fnsendBirthdayEmail(contact.Attributes["fullname"].ToString(), contact.Attributes["emailaddress1"].ToString());
                }
            }



fnsendBirthdayEmail:-

        public static void fnsendBirthdayEmail(string fullname, string ContactEmail)
        {
            string myEmail = "sam@gmail.com";
            string myPassword = "password";
            string subject = "Birthday Wish!";
            string body = "Dear " + fullname + " wish you a very happy birthday.!!!";
            SmtpClient client = new SmtpClient();
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            client.Timeout = 10000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(myEmail, myPassword);
            MailMessage _MailMessage = new MailMessage(myEmail, ContactEmail, subject, body);
            _MailMessage.BodyEncoding = UTF8Encoding.UTF8;
            _MailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            client.Send(_MailMessage);
        }


Now Publish it:-

1.       Click on Publish.


2.       Select Create New and then OK


3.       Put App Name and other field values as shown in below screenshot and then click on Create.


4.       Now we can check the function app URL in Site URL.


5.       After completion we can check in Azure.



Let check its working:-



Now let’s check email.




Web Resource vs PCF vs Canvas App - which of the one is used?

While started working on specific Business Requirements related to custom layout, there is a always common question that "where to star...