Introduction

Sending recurring emails is important for keeping in touch with customers and stakeholders. Hangfire in .NET Core Minimal APIs make it easy to automate this process.

What is hangfire?

Hangfire is an open-source and well-documented task scheduler for ASP.NET and ASP.NET Core and is completely free for commercial use. It's multi-threaded, easily scalable, and offers a variety of job types. It's well-structured, simple to use, and gives a powerful performance.

Why is hangfire used?

It is mainly used to perform background tasks such as batch/email notification, batch import of files, video/image processing, database maintaining, file purging, etc. Hangfire can be used for background tasks with high/low CPU consumption, short/long-running tasks, recurring jobs, fire and forget, and many more.

Different types of jobs in hangfire

Different types of jobs are available in Hangfire.

Let's create a new ASP.NET Core Web API 8 project in Visual Studio 2022.

Create a new ASP dot NET core

Implementation of the SendMail message integration.

Install the below NuGet packages

MailKit

Mailkit

The main configuration setup for your application's appsettings.json file.

  "EmailSettings": {
    "Email": "yourfrommail",
    "Password": "gmailapppaswords",
    "Host": "smtp.gmail.com",
    "Displayname": "SampleDisplayName",
    "port": 587
  }

EmailSettings Configuration

We create the EmailSettings class.

Code

MailRequest Class

Create purpose of Sendmail function request Object.

Create purpose of sendmail function

public async Task SendMail(MailRequest mailrequest)
{
    var email = new MimeMessage();
    email.Sender = MailboxAddress.Parse(emailSettings.Email);
    email.To.Add(MailboxAddress.Parse(mailrequest.ToMail));
    email.Subject = mailrequest.Subject;
    var builder = new BodyBuilder();
    builder.HtmlBody = mailrequest.Body;
    email.Body = builder.ToMessageBody();
    using var smtp = new SmtpClient();
    smtp.Connect(emailSettings.Host, emailSettings.Port, MailKit.Security.SecureSocketOptions.StartTls);
    smtp.Authenticate(emailSettings.Email, emailSettings.Password);
    await smtp.SendAsync(email);
    smtp.Disconnect(true);
}

Step 1

Step 2

Step 3

In this section, the 'ToMail' address specifies the recipient of the email, while the 'Subject' line indicates the heading related to our templated content. The 'Body' of the email is constructed using an HTML string template.

The GetHtml() function generates the HTML template used for composing the body of the email. This template contains the formatted content and styling that will be included in the email message, such as information about the offer ending soon. The generated HTML serves as the visual representation of the email's content when it is received by the recipient.

Note. Integrate Hangfire into the .NET application

Install the below NuGet packages

The configuration setup for Hangfire in the Program.cs file.

builder.Services.AddHangfire(configuration => configuration
    .UseSimpleAssemblyNameTypeSerializer()
    .UseRecommendedSerializerSettings()
    .UseInMemoryStorage());

Run the dotnet run command to start the app.

Now, navigate to the URL shown in the console, e.g., http://localhost:5026, to view the Hangfire dashboard.

Overview

The result in the recipient email should look like this.

Get started with gmail

This is the final output for recurring jobs in email push notifications every 15 seconds.

Limited time offer ending soon

.NET Core Minimal API Source Link: https://github.com/jobin4jo/MinimalAPI_For_HangFire.

Summary

In this article, we looked at Hangfire, which we have used in .NET 8 minimal API to schedule background jobs. Hoping you understand the things related to Hangfire.

Using Hangfire with .NET Core Minimal APIs makes it easy to schedule and send recurring emails. This helps organizations communicate effectively and engage with their audience. By automating these processes, businesses can improve efficiency and provide better experiences for users.