Hello everyone please let me know. I have 100 users in the database, I want all these users to be emailed in bulk. I have done the work of sending the email. Now I have a query that as soon as I click on the button with my email center, the service should continue running in the background, which means the user can click and do something in the application. in.net core with razor pages
Loading

Amit MohantyRank #112 · 20K reputationPosted May 29, 2023, 6:10 AM EDT
Create a new class that inherits from BackgroundService. This class will contain the logic for sending the emails. Let's call it "EmailSenderService.cs":
using Microsoft.Extensions.Hosting; using System; using System.Threading; using System.Threading.Tasks; public class EmailSenderService : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { // Your email sending logic goes here while (!stoppingToken.IsCancellationRequested) { // Send emails to users await SendBulkEmails(); // Wait for a certain interval before sending the next batch of emails await Task.Delay(TimeSpan.FromMinutes(10), stoppingToken); } } private async Task SendBulkEmails() { // Retrieve the list of users from the database List users = GetUsersFromDatabase(); // Loop through the users and send emails foreach (User user in users) { // Send email to user await SendEmail(user.Email, "Subject", "Message"); } } private List GetUsersFromDatabase() { // Retrieve the list of users from the database // Implement the logic to fetch the users from the database and return them as a list } private async Task SendEmail(string email, string subject, string message) { // Implement the logic to send an email to the specified email address } }In your Razor Page or Controller, inject the IHostedService interface and start the email sender service when the button is clicked.
public class IndexModel : PageModel { private readonly IHostedService _emailSenderService; public IndexModel(IHostedService emailSenderService) { _emailSenderService = emailSenderService; } public async Task OnPostSendEmailsAsync() { // Start the email sender service await _emailSenderService.StartAsync(default); // Return a response indicating that the emails are being sent in the background return Content("Emails are being sent in the background."); } }Jaimin ShethiyaRank #177 · 13.7K reputationPosted Aug 22, 2023, 1:44 AM EDT
Hello Sunny,
You can use here the Azure service bus or Rabbit MQ concepts like Publisher and Subscriber.
When you have clicked on the button that time you need to publish the messages.
Another side Azure functions have to consume the message of whatever you have published and inside that you need to decorate the email body and everything rest things required to send the email and send the mail to the users.
Mohammad HussainRank #513 · 4.8K reputationPosted Aug 24, 2023, 12:36 AM EDT
To achieve background processing for sending bulk emails in an ASP.NET Core Razor Pages application, you can use a combination of background tasks and asynchronous programming. One popular approach is to use the built-in
BackgroundServiceclass along with a message queue system like RabbitMQ or Azure Service Bus. However, for simplicity, I'll demonstrate using theIHostedServiceinterface and a hosted service background task.Sourabh DhimanRank #2,987 · 483 reputationPosted May 29, 2023, 6:59 AM EDT
Sir it's not working.
Cr BhargaviRank #420 · 5.9K reputationPosted Aug 24, 2023, 1:39 AM EDT
Develop a background service class that handles email sending asynchronously and then Register the background service in the
Startup.csusingAddHostedService. Post that Implement theExecuteAsyncmethod in the background service to send emails periodically.In your Razor Page's code-behind, initiate the background service when users click the email send buttonPrasad RaveendranRank #320 · 7.8K reputationPosted Aug 26, 2023, 10:15 PM EDT
I'm curious to know what was your technical solution to achieve this. If its HostedService or BackGround Service, please confirm whether this has been registed in Starup.cs or in Program.cs depends on your .NET Version you are following