System.Reflection.TargetInvocationException - C#

Hello everyone

I’m trying to send emails using SmtpClient. Very simple.

public bool SendEmail()
{
    try
    {

        MailMessage message = new MailMessage();
        SmtpClient smtp = new SmtpClient();

        message.From = new MailAddress("noreply@domain.com");
        message.To.Add(new MailAddress("myemail@hotmail.com"));
        message.Subject = "Test Hangfire";
        message.Body = "<b>Sending test.</b>";
        message.IsBodyHtml = true;

        smtp.Port = 587;
        smtp.Host = "mail.mydomain.com.br";
        smtp.EnableSsl = false;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential("noreply@muydomain.com", "myPWD");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.Send(message);

        return true;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Works fine without hangfire.

But, if I include a hangfire method, an error ocurred.

**Failed**

An exception occurred during processing of a background job.

System.Reflection.TargetInvocationException
Uma exceção foi acionada pelo destino de uma chamada.

System.Reflection.TargetInvocationException: Uma exceção foi acionada pelo destino de uma chamada. —> System.NullReferenceException: Referência de objeto não definida para uma instância de um objeto.
em System.Web.UI.Page.get_Server()
em ASP.default_aspx…ctor()
— Fim do rastreamento de pilha de exceções internas —
em System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
em System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
em System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
em System.Activator.CreateInstance(Type type, Boolean nonPublic)
em System.Activator.CreateInstance(Type type)
em Hangfire.JobActivator.ActivateJob(Type jobType)
em Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type)
em Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)
em Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_0.b__0()
em Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func1 continuation) em Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_1.<PerformJobWithFilters>b__2() em Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable1 filters)
em Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)
em Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection connection, String jobId)

My button_click code:

BackgroundJob.Enqueue(() => SendEmail());

Updating my status.

All tasks returns this same error :frowning:

Failed
An exception occurred during processing of a background job.
System.Reflection.TargetInvocationException

This issue has been resolved including all tasks in same Assembly.

How this solution can be done exactly? Thanks for help.

1 Like