My concern is: have a RecurringJon that sends an email on a daily basis. in static email content, it is working fine. but my end-user want that their critical count of items will be sent to them daily. I have a controller for their inventory that compares items to determine which items are at a critical level. but I can't call the _context of the inventory.
I've already tried to add the context in the service but still not working.
ItemRegContext
namespace Intranet.Controllers
{
public class ItemRegController : Controller
{
private readonly ItemRegContext _context;
public ItemRegController(ItemRegContext context)
{
_context = context;
}
}
Startup.cs
public void Configure(IApplicationBuilder app, IRecurringJobManager recurringJob)
{
app.UseHangfireDashboard();
recurringJob.AddOrUpdate("", () => SendEmail(), Cron.Minutely());
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
public void SendEmail()
{
var message = new MimeMessage();
var builder = new BodyBuilder();
string msgFromDB = string.Empty;
var items = _context.ItemRegs;
foreach (ItemReg item in items)
{
if (item.CritLevel >= item.Qty)
{
msgFromDB += "<tr>" +
"<td>" + item.ItemName + "</td>" +
"<td>" + item.ItemDesc + "</td>" +
"<td>" + item.ManufName + "</td>" +
"<td>" + item.AsstSerial + "</td>" +
"<td>" + item.PartNum + "</td>" +
"<td>" + item.TypeName + "</td>" +
"<td>" + item.CalDate + "</td>" +
"<td>" + item.Qty + "</td>" +
"<td>" + item.UnitName + "</td>" +
"<td>" + item.Remarks + "</td>" +
"<td>" + item.LocName + "</td>" +
"</tr>";
}
//else {
// msgFromDB += "<tr><<td colspan='11'>No critical record on the list</td>></tr>";
//}
}
#region email
// reference value from appsettings.json
string host = "smtp.some.com";
int port = 333;
bool boole = false;
string authEmail = "[email protected]";
string authPass = "somepassword";
message.From.Add(new MailboxAddress("somename", "[email protected]"));
message.To.Add(new MailboxAddress("[email protected]"));
message.Subject = "some subjects";
builder.HtmlBody =
string.Format(@"
<p> Critical Stocks for today " +
DateTime.Now.ToString() + " </p>" +
"<table border='1'> " +
"<thead>" +
" <tr> " +
" <th> Name </th>" +
" <th> Item Description </th>" +
" <th> MFR </th>" +
" <th> Asset/SN </th>" +
" <th> P/N </th>" +
" <th> Type </th>" +
" <th> CAL Date </th>" +
" <th> QTY </th>" +
" <th> Unit </th>" +
" <th> Remarks </th>" +
" <th> Location </th>" +
" </tr>" +
" </thead>" +
" <tbody>" + msgFromDB + "</tbody> " +
"</table>" +
"<br />" +
"<br />");
message.Body = builder.ToMessageBody();
using (var client = new SmtpClient())
{
client.Connect(host, port, boole);
client.Authenticate(authEmail, authPass);
client.Send(message);
client.Disconnect(true);
}
}
on the public void SendEmail()
, var items = _context.ItemRegs;
the _context should get the list of ItemRegContext
This does exactly what I needed. https://jonhilton.net/simple-background-jobs-with-hangfire-and-aspnet-core/