title: Net Core send email background service with Postal and Hangfire
body: <p>I'm trying to send emails using Hangfire and Postal, but I'm having problems with the HTTPContext. If I execute the shooting through a controller the sending is done without problems, now if I execute a job via Hangfire an expection is generated.</p>
  <p>Code startup.cs</p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddHangfire(configuration =&gt; configuration
          .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
          .UseSimpleAssemblyNameTypeSerializer()
          .UseRecommendedSerializerSettings()
          .UseSqlServerStorage(Configuration.GetConnectionString(&quot;default&quot;), new SqlServerStorageOptions
          {
              CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
              SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
              QueuePollInterval = TimeSpan.Zero,
              UseRecommendedIsolationLevel = true,
              UsePageLocksOnDequeue = true,
              DisableGlobalLocks = true
          }));
  
      services.Configure&lt;EmailSenderOptions&gt;(Configuration.GetSection(&quot;EmailSender&quot;));
      services.AddPostal();
      services.AddTransient&lt;IEmailSenderEnhance, EmailSender&gt;();
      services.AddHttpContextAccessor();
      services.AddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
  }
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
      var options = new DashboardOptions
      {
          Authorization = new[] { new HangFireAuthorizationFilter() }
      };
  
      app.UseHangfireDashboard(&quot;/hangfire&quot;, options);
      app.UseHangfireServer();
  
      RecurringJob.AddOrUpdate&lt;NotificationServices&gt;(&quot;SendNotify-&quot;, m =&gt; m.Notify(1), Cron.Daily);
  }
  </code></pre>
  <p>Code NotificationServices.cs</p>
  <pre><code>public class NotificationServices
  {
      private readonly IEmailSenderEnhance _emailSender;
      private readonly Config _appConfig;
      private readonly IHttpContextAccessor _httpContextAccessor;
  
      public NotificationServices(IEmailSenderEnhance emailSender, IOptions&lt;Config&gt; optionsAccessor, IHttpContextAccessor httpContextAccessor)
      {
          _emailSender = emailSender;
          if (optionsAccessor == null) throw new ArgumentNullException(nameof(optionsAccessor));
          _appConfig = optionsAccessor.Value;
          _httpContextAccessor = httpContextAccessor;
      }
  
  
      public void Notify(int messageId)
      {
          var listOSs = Task.Run(async () =&gt; await this.SendEmail(messageId));
      }
  
      public async Task SendEmail(int orderID)
      {
          var orderRy = new OrderRepository();
          var order = orderRy.Get_By_ID(orderID);
  
          try
          {
              var requestPath = new Postal.RequestPath
              {
                  PathBase = _httpContextAccessor.HttpContext.Request.PathBase.ToString(),
                  Host = _httpContextAccessor.HttpContext.Request.Host.ToString(),
                  IsHttps = _httpContextAccessor.HttpContext.Request.IsHttps,
                  Scheme = _httpContextAccessor.HttpContext.Request.Scheme,
                  Method = _httpContextAccessor.HttpContext.Request.Method
              };
  
              var emailData = new Postal.Email(&quot;SendTest&quot;)
              {
                  RequestPath = requestPath,
              };
  
              var emailsCopy = $&quot;{order.Salesman.Email},{order.Salesman2.Email},&quot;;
  
              emailData.ViewData[&quot;to&quot;] = emailsCopy;
              emailData.ViewData[&quot;Order&quot;] = order;
  
              await _emailSender.SendEmailAsync(emailData);
          }
          catch (Exception ex)
          {
              
          }
      }
  }
  </code></pre>
  <p>The error is in HttpContext:
  _httpContextAccessor.HttpContext.Request.PathBase.ToString()</p>
  <p>Is it possible to send e-mails via background with Postal?</p>