Email Jobs Succeeding But Not Sending

I can send emails using the method when not using hangfire.

When the method is called from hangfire it is shown as succeeded but the email doesnt send.

BackgroundJob.Schedule(
        () => Email(Subject),
        TimeSpan.FromMinutes(1)
        );
public void Email(string Subject)
    {

       
        try
        {
            //Initialize WebMail
            WebMail.SmtpServer = "relay.example.com";
            WebMail.SmtpPort = 587;
            WebMail.SmtpUseDefaultCredentials = false;
            WebMail.UserName = "xxxx";
            WebMail.Password = "xxxx";
            WebMail.From = "\"Reports\" <Reports@example.com>";

            //Send Email
            WebMail.Send(to: "client@example.com",
                subject: Subject,
                body: "Test Email"
            );

        }
        catch (Exception e)
        {
        }
    }

The username, password, and relay service have been removed for security as has the actual email addresses.

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.

Removed the try/catch and it still “succeeded” without actually sending the email.

Ok there is an occasional fail now.

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.

BackgroundJob.Schedule<WebMail>(
            () => Email(Subject),
            TimeSpan.FromMinutes(1)
            );

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.

Reading the documentation for WebMail I notice the following:

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.

I’m pretty sure this is the problem.

Thanks for the help I found a cheeky work around by using hangfire to call a post request to the page that actually processes the email itself.

EDIT: NVM workaround was a false positive due to an extra line that was left in from testing

Cheeky indeed!

I would defently spend 5 mins to find an alternative solution, like using the SmtpClient instead.

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.

If I knew it was a contest, I should have payed more attention :stuck_out_tongue:

Good you’ve found a solution.

1 Like