I have a MVC application and I am trying to send an email using Hangfire and Postal. The email must be sent after a registration. The registration works properly, but the job I run remain enqueued and I not receive any email. So in my MVC controller I have the following code:
public async Task<ActionResult> Register(RegisterViewModel model)
{
//register correctly the user
//I send the email
BackgroundJob.Enqueue(() =>
NotifyRegistration(user.Id, user.UserName, user.Email)
);
...
}
[AutomaticRetry(Attempts = 5)]
public async Task NotifyRegistration(string userId, string username, string email)
{
//I calculate callbackUrl
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
var emailService = new EmailService(engines);
var emailToSend = new NewRegisteredUserEmail
{
To = email, UserName = username, CallbackUrl = callbackUrl
};
emailService.Send(emailToSend);
}
I cannot debug the NotifyRegistration method. I don't know why. I am using Postal, so EmailService is not my implementation. Here how I configured the smtp service:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.live.com" port="25" enableSsl="true" userName="***" password="***"></network>
</smtp>
</mailSettings>
</system.net>
If I run the hangfire dashboard I see the jobs enqued
But nothing else happened. What do I miss to send the email?
Thank you
UPDATE In the startup.cs I have written this:
var options = new SqlServerStorageOptions
{
QueuePollInterval = TimeSpan.FromSeconds(1)
};
GlobalConfiguration.Configuration
.UseSqlServerStorage("DbConnectionString", options)
.UseFilter(new LogEmailFailureAttribute());
app.UseHangfireDashboard();
app.UseHangfireServer();
UPDATE 2 I transformed my NotifyRegistration in this way:
[AutomaticRetry(Attempts = 5)]
public async Task NotifyRegistration(string userId, string username, string email, EmailService emailService)
{
//I calculate callbackUrl
var emailToSend = new NewRegisteredUserEmail
{
To = email, UserName = username, CallbackUrl = callbackUrl
};
emailService.Send(emailToSend);
}
I Found the problem(s):
The version of sql server was not supported. I was using 2005. Supported database is 2008R2 and later: http://docs.hangfire.io/en/latest/configuration/using-sql-server.html
The method NotifyRegistration must be static: https://discuss.hangfire.io/t/jobs-in-enqueue-state-most-never-run/2367/4
.
[AutomaticRetry(Attempts = 5)]
public static void NotifyRegistration(string userId, string username, string email, EmailService emailService)
{
//I calculate callbackUrl
var emailToSend = new NewRegisteredUserEmail
{
To = email, UserName = username, CallbackUrl = callbackUrl
};
emailService.Send(emailToSend);
}
Without seeing your Hangfire configuration...
Do you have app.UseHangfireServer();
anywhere? That is what tells Hangfire that it needs to do the executing - otherwise you're simply queuing as it expects something else to do the execution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With