title: Sending emails using Hangfire on asp.net core 2.1
body: <p>I have set up Hangfire correctly. I am able to run the following code from postman:</p>
  
  <pre><code> [HttpPost("appointments/new")]
   public async Task&lt;IActionResult&gt; SendMailMinutely()
   {
       RecurringJob.AddOrUpdate(() =&gt; Console.WriteLine("Recurring!") Cron.Minutely);
       await Task.CompletedTask;
       return Ok();
   }
  </code></pre>
  
  <p>When I hit this API point, this works fine. 
  What I would like to do is to run my email controller using the same code above. My modified <code>SchedulersController</code> code is:</p>
  
  <pre><code>[Route("/api/[controller]")]
  public class SchedulersController : Controller
  {
      private readonly MailsController mail;
      public SchedulersController(MailsController mail)
      {
          this.mail = mail;
      }
  
      [HttpPost("appointments/new")]
      public async Task&lt;IActionResult&gt; SendMailMinutely()
      {
          RecurringJob.AddOrUpdate(() =&gt; mail.SendMail(), Cron.Minutely);
          await Task.CompletedTask;
          return Ok();
      }
  }
  </code></pre>
  
  <p>And My <code>MailsController</code> is:</p>
  
  <pre><code>[HttpPost("appointments/new")]
  public async Task&lt;IActionResult&gt; SendMail()
   {
      var message = new MimeMessage ();
      message.From.Add (new MailboxAddress ("Test", "test@test.com"));
      message.To.Add (new MailboxAddress ("Testing", "test@test123.com"));
      message.Subject = "How you doin'?";
  
      message.Body = new TextPart ("plain") {
          Text = @"Hey Chandler,
                  I just wanted to let you know that Monica and I were going to go play some paintball, you in?
                  -- Joey"
      };
  
       using (var client = new SmtpClient ()) {
       client.ServerCertificateValidationCallback = (s,c,h,e) =&gt; true;
  
      client.Connect ("smtp.test.edu", 25, false);
  
      await client.SendAsync (message);
              client.Disconnect (true);
          }
  
  
          return Ok();
      }
  </code></pre>
  
  <p>The error message I receive is:</p>
  
  <blockquote>
    <p>An unhandled exception has occurred while executing the request.
    System.InvalidOperationException: Unable to resolve service for type 'Restore.API.Controllers.MailsController' while attempting to activate 'Restore.API.Controllers.SchedulersController'</p>
  </blockquote>
  
  <p>How can I use my <code>MailsController</code> so that I can schedule emails to send using Hangfire?
  Any help will be greatly appreciated.</p>