First thing I would try is to remove the empty catch block.
If anything fails during sending, this block will swallow that error and the job will not be able to report the error.
System.NullReferenceException
Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.WebPages.Scope.AspNetRequestScopeStorageProvider.get_RequestScopeInternal()
at System.Web.WebPages.Scope.AspNetRequestScopeStorageProvider.get_CurrentScope()
at System.Web.Helpers.WebMail.set_SmtpServer(String value)
I have tried to pass the WebMail type to it but that throws a compiler error about not allowing static types.
I see the stack contains request related stuff. I’m not sure how this WebMail thing works, but what I do know for sure, is that when the hangfire job is executed, there is no request scope.
Remarks
This class represents a helper, which is a component that simplifies web programming in ASP.NET Web Pages. You can use the WebMail class to send email messages from a web application.
I’m not sure how you have setup Hangfire, but in most cases, the execution of jobs will not happen in a ASP.NET Web Pages application. Typically, your web app will enque the job, and the job will be executed in a service, worker role, long-running console app or whatever.
Thanks for the help. Someone over on Stackoverflow beat you to the SMTP By about 45min. I have it working great now. Thank you though. (WebMail uses smtp internally)
BUT-
The WebMail class is intended for usage in ASP.NET Web Pages context. When used outside that context it probably doesn’t have everything it needs, and thus you will get a NullReferenceException.