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);
}