Sending email using SMTPClient in hangfire is not working

I’m using Hangfire to process a time consuming job. Its working well, but the problem is I need to send notifications after the job is finished. For that I used SMTPClient at the end of this method to shoot out an email once completed. The job is succeeded but there is no email. I’m not sure what the issue is.

Technology background: The actual job method and hangfire methods are residing in two seperate WCF service layer. TaskShceduler is a WCF service that enqueue items for hangfire processor. And internally the enqueued method is calling another WCF to process the actual job.

TaskScheduler WCF:
public void EnqueueMergeTask(some params)
{…
BackgroundJob.Enqueue(() => EnqueueItems(items, Filters, notificationEmail));
}

public void EnqueueItems(List items, Dictionary<string, List> Filters, string notificationEmail)
{
// calls another WCF method
SPOperations.MergeReportData(items, Filters, notificationEmail);
}

SPService WCF:
public void MergeReportData(List ietms, Dictionary<string, List> Filters, string notificationEmail)
{…

                    MailMessage msg =
                        new MailMessage(fromaddr, toaddr, "Merge Reports Task status", body);
                    msg.IsBodyHtml = true;
                    SmtpClient smtp =
                        new SmtpClient(
                            "XXX.XXX.XXX.XXX");
                    smtp.Send(msg);

}

For some reasons the mail is not send out. Job is getting processed, no exceptions and I’m getting the actual output except for the email part. The same code works when I call the method directly NOT using hangfire. so i’m not sure if there anything else that I should be aware of in this case.

Appreciate the help!

Thanks

Are you sure no exception / stacktrace? Could it be serialization problem? Due to the way MailMessage was implemented my Microsoft, it cannot be serialized.

If you are using SQL Server, check column [InvocationData] in [HangFire].[Job] table and see if anything gets serialized at all.

Refer to my answer here hope it helps: Help Sending Email (MailMessage Deserialize Problem)