10 Nov’18

D365 GeneralSend Custom Emails using SendGrids API

Introduction:

In this blog we will demonstrate how to send custom emails using SendGrids API.

Scenario:

If we want to send custom emails to customers stored in Dynamics 365 with different email body for every customer based on their data stored in CRM, it is not possible to do so by using the normal Email Editor as it has some limitations while displaying dynamic content. One way of doing this is by creating a console app which will use SendGrids API to send out custom emails for every customer.

Pre-Requisites:

  1. Visual Studio
  2. SendGrid Account

Implementation:

Step 1: For this demonstration we will see how to send out a mail to a single email. First we create a Console App in Visual Studio

Console App

Step 2: Once the Project is created Right Click on the Project and select Manage Nuget Packages

ManageNuget

Browse to the latest Nuget packages and install SendGrid package

Browse Latest Nuget Package

Step 3: Next we have to create an API Key in SendGrid. Just give a name for a key and Click on Create Key which will generate a unique key. Store this key which will be used in the Code

Step 4: Below is the Code to send email:

using SendGrid;
using SendGrid.Helpers.Mail;
using System.Threading.Tasks;
namespace SendGridConsoleApp
{
class Program
{
static void Main(string[] args)
{
string sendGridAPIKey = "EnterKeyHere";
Execute(sendGridAPIKey).Wait();
}
static async Task Execute(string _apiKey)
{
var client = new SendGridClient(_apiKey);
var from = new EmailAddress("test@gmail.com", "From UserName");
var subject = "MAIL Send Through SendGrid";
var to = new EmailAddress("test@gmail.com", "To UserName");
var plainTextContent = "Example PlainText";
var htmlContent = @"<!DOCTYPE html><html><head><style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2
}
th {
background-color: #4CAF50;
color: white;
}
</style></head><body><h2>Important Details</h2>
<table><tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email Address</th></tr>
<tr><td>Peter</td><td>Griffin</td><td>pgriffin@gmail.com</td></tr>
<tr><td>Lois</td><td>Griffin</td><td>Lois@gmail.com</td></tr><tr>
<td>Joe</td><td>Swanson</td><td>joe@gmail.com</td>
</tr><tr><td>Cleveland</td><td>Brown</td>
<td>clev@gmail.com</td></tr></table></body></html>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
}}}

Step 5: Once the Console App is run the email is send below shown is the sample of the Email sent

While running this for multiple customers we can create different HTML body content for each customer and pass it to the CreateSingleEmail function.This approach can also be used to send bulk emails to customers.

Written by

Clinton D'Mello

Software Engineer

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • Performing Update operation in Microsoft Dynamics NAV through Integration in Scribe Online

    13 November’ 2019

    Introduction: Use the Update Block to update existing records in the source or target datastore. While integrating w...

    Read more
  • Adding Microsoft Dynamics NAV Connection in Tibco Scribe Online

    13 November’ 2019

    Introduction: Establishing a connection is an important step before developing a solution for Integration. While integra...

    Read more
  • Time zone Conversion from Microsoft D365 for SSRS Reporting

    6 November’ 2019

    Introduction: Converting Date/Time values according to a time zone is quite challenging task in SSRS Reporting. For D365...

    Read more
  • How to Synchronize Office 365 and Zoho People?

    4 November’ 2019

    If your organization is using Zoho People and Office 365, you will find this article very useful. In this article we are...

    Read more
  • Check Plugin Timeout issues using Tracing in D365 CE

    31 October’ 2019

    In Dynamics plugin implementations, you might have lengthy complex plugins that makes calls to Dynamics several times. I...

    Read more