Marius Vasile

Marius Vasile

  • 590
  • 1.9k
  • 149k

asp.net core Background Task scheduler

Oct 10 2022 3:28 PM

I have a background task to send emails that work but it works continously. I want to make it fire at a spoecific time of day. How do I do that?

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{

    var dataAzi = DateTime.Now;
    var data = await _context.MemoratClassDetails.Include(s => s.MemoratClassSubCategory)
                                                    .ThenInclude(s => s.MemoratClassCategory)
                                                    .Where(s => s.MemDataReminder.Year == dataAzi.Year && s.MemDataReminder.Month == dataAzi.Month && s.MemDataReminder.Day == dataAzi.Day && s.Reminder == false)
                                                    .ToListAsync();
    while (!stoppingToken.IsCancellationRequested)
    {
        if (data.Count() > 0)
        {
            foreach (var i in data)
            {
                var category = i.MemoratClassSubCategory.MemoratClassCategory.MemCategory;
                var uemail = "[email protected]";
                var subcategory = i.MemoratClassSubCategory.MemSubCategory;
                var message = i.MemData;

                var emailCom = new MimeMessage();
                emailCom.From.Add(new MailboxAddress(_smtpSettings.SenderName, _smtpSettings.SenderEmail));
                emailCom.To.Add(new MailboxAddress(uemail));
                emailCom.Cc.Add(new MailboxAddress("[email protected]"));
                emailCom.Subject = "Reminder!";



                var bodybuilder = new BodyBuilder();
                bodybuilder.HtmlBody = @"
                <div style='background-color:white;border-color:darkblue;border-style:double;width:80%; color:darkblue;padding:20px;margin:10px'>
                    <h1 style='text-align: center; font-family: Sigmar One' , cursive;'>Contact Notification</h1>
                    <hr>
                    <br><h3 style='color:blueviolet;'>Categorie: </h3>" + category + @"
                    <br>
                    <br><h3 style='color:blueviolet;'>Subcategorie: </h3>" + subcategory + @"
                    <br>
                    <br><h3 style='color:blueviolet;'>Detaliu: </h3>" + message + @"
                    <br><br><br><br><br>
                    <p style='text-align:center'>
                        This is a message received because you set a reminder on rosafety.com
                    </p>
                </div>
                ";

                emailCom.Body = bodybuilder.ToMessageBody();

                using (var client = new SmtpClient())
                {
                    await client.ConnectAsync(_smtpSettings.Server, _smtpSettings.Port, true);

                    client.AuthenticationMechanisms.Remove("XOAUTH2");

                    await client.AuthenticateAsync(_smtpSettings.UserName, _smtpSettings.Password);
                    await client.SendAsync(emailCom);
                    await client.DisconnectAsync(true);
                }

                var mdid = i.MemoratDetID;
                var editData = await _context.MemoratClassDetails.FindAsync(mdid);
                editData.Reminder = true;
                await _context.SaveChangesAsync();
            }
        }

    }
}

 


Answers (5)