Emails - SendGrid
Managing email communication is a crucial aspect of any modern business or application. LeanCode CoreLibrary streamlines the process of sending emails by integrating with SendGrid, a widely acclaimed and trusted email delivery system. With LeanCode CoreLibrary's custom integration with SendGrid, you gain access to a simplified and efficient email delivery solution. For more information, you can explore SendGrid documentation and the official SendGrid C# library GitHub repository.
Packages
| Package | Link | Application in section |
|---|---|---|
| LeanCode.SendGrid | Configuration | |
| LeanCode.ViewRenderer.Razor | .cshtml templates |
|
| SendGrid | Email sending |
Configuration
To incorporate SendGrid into your LeanCode CoreLibrary-based application, follow the example below. This will enable the SendGrid client, allowing you to send emails through .cshtml template files. The example assumes that templates are located in the Templates folder.
// . . .
private static readonly RazorViewRendererOptions ViewOptions = new("Templates");
// . . .
public override void ConfigureServices(IServiceCollection services)
{
// . . .
services.AddRazorViewRenderer(ViewOptions);
// Add SendGrid ApiKey
services.AddSendGridClient(new SendGridClientOptions { ApiKey = "" });
// . . .
}
Add this code to the startup project to ensure that email templates in the Templates folder are included in the output directory and preserved when the project is built:
<ItemGroup>
<None Include="Templates/**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Remove="Templates/**" />
</ItemGroup>
Sending emails
Suppose you need to send an email to an employee to inform them of their assignment, with the assignment name as a parameter. For this purpose you can create a EmployeeAssignedToAssignmentEmail.cs class:
public class EmployeeAssignedToAssignmentEmail
{
public string AssignmentName { get; set; }
}
You should also define a EmployeeAssignedToAssignmentEmail.cshtml template file inside Templates directory with the same name as model above to ensure proper linkage:
<div>
You have been assigned to @Model.AssignmentName assignment.
<div>
After configuring as shown above, you can start sending emails using SendGrid with the following code. Make sure to verify your sender address in SendGrid:
// . . .
private const string FromEmail = "no-reply@leancode.pl";
private const string FromName = "LeanCode";
private readonly SendGridRazorClient sendGridClient;
// . . .
public async Task SendEmployeeAssignedToAssignmentEmailAsync(
Employee employee,
Assignment assignment,
CancellationToken cancellationToken)
{
var vm = new EmployeeAssignedToAssignmentEmail
{
AssignmentName = assignment.Name
};
var message = new SendGridRazorMessage()
.WithSubject("You have been assigned to assignment")
.WithSender(FromEmail, FromName)
.WithRecipient(employee.Email)
.WithHtmlContent(vm);
await sendGridClient.SendEmailAsync(message, cancellationToken);
}