title: How to use a service declared on Startup.cs outside controllers
body: <p>I am developping a web application using ASP.net core, I am new to this technology.</p>
  
  <p>I am using a service to send emails, here is the class :</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.Extensions.Options;
  using System.Net.Mail;
  
  
  
  
  namespace WebApplication.Services
  {
  
      public class MessageSender : IEmailSender, ISmsSender
      {
          public MessageSender(IOptions&lt;MessageSenderOptions&gt; optionsAccessor)
          {
              Options = optionsAccessor.Value;
          }
  
          public MessageSenderOptions Options { get; } //set only via Secret Manager
  
          public Task SendEmailAsync(string email, string subject, string message)
          {
              // Plug in your email service here to send an email.
  
              using (var smtp = new SmtpClient(Options.emailServer))
              {
                  var mail = new MailMessage
                  {
                      Subject = subject,
                      IsBodyHtml = true,
                      From = new MailAddress(Options.fromEmail, Options.fromName),
                      Body = message
                  };
  
                  if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != "Development")
                  {
                      mail.To.Add(email);
                  }
                  else
                  {
  
                      mail.To.Add("test@gmail.com");
                  }
  
  
  
                  smtp.Send(mail);
              }
  
              return Task.FromResult(0);
          }
  
          public Task SendSmsAsync(string number, string message)
          {
              // Plug in your SMS service here to send a text message.
              return Task.FromResult(0);
          }
      }
      public class MessageSenderOptions
      {
          public List&lt;string&gt; emails { get; set; }
          public string emailServer { get; set; }
          public string fromEmail { get; set; }
          public string fromName { get; set; }
  
  
      }
  
  }
  </code></pre>
  
  <p>And I am calling it in my controller this way :</p>
  
  <pre><code>namespace WebApplication.Controllers
  {
      [Route("api/[controller]")]
      public class MyController : Controller
      {
  
          private readonly Context _Context;
          private IHostingEnvironment _environment;
          private ILogger&lt;MyController&gt; _logger;
          private MessageSender _messageSender;
  
          public MyController(IOptions&lt;RegistrarConnection&gt; optionsAccessor,
              Context dbContextd,
              IHostingEnvironment environment,
              ILogger&lt;GestionAteliersController&gt; logger,
              MessageSender messageSender,
  
              )
          {
              Options = optionsAccessor.Value;
  
              _Context = dbContextd;
              _environment = environment;
              _logger = logger;
              _messageSender = messageSender;
  
          }
  
    [Route("[action]")]
          public IActionResult save([FromBody] Obj obj)
          {
              try
  
              {
                  //code
                  string subjet = "Subject";
  
                  string message = "Message";
  
                  _messageSender.SendEmailAsync("test@gmail.ca", subjet, message);
  
                  var v = new { result = "OK" };
  
                  return this.Ok(v);
              }
              catch (Exception ex)
              {
                  _logger.LogError(ex.ToString());
                  return new BadRequestObjectResult("Error");
              }
          }
  
  
  </code></pre>
  
  <p>It's just a part of the method, and everything works fine.</p>
  
  <p>The problem that I have now is that I want to call this method </p>
  
  <pre><code>_messageSender.SendEmailAsync("test@gmail.ca", subjet, message);
  </code></pre>
  
  <p>outside the controller, in another class and I don't know hot to use it as the controller do easily.</p>
  
  <p>I don't understand how it's using the constructor of the class <strong>MessageSender</strong>.</p>
  
  <p>In the <strong>Startup.cs</strong> file there is this part of code in the method <strong>ConfigureServices</strong> : </p>
  
  <pre><code>services.Configure&lt;MessageSenderOptions&gt;(Configuration.GetSection("MessageSenderOptions"));
  
              services.AddTransient&lt;IEmailSender, MessageSender&gt;();
              services.AddTransient&lt;ISmsSender, MessageSender&gt;();
              services.AddTransient&lt;MessageSender, MessageSender&gt;();
  
  </code></pre>
  
  <p>And in my file <strong>appsettings.json</strong> there is this part of code :</p>
  
  <pre><code>"MessageSenderOptions": {
      "emailServer": "smtp.gmail.ca",
      "emails": [
        "test@gmail.ca"
      ],
      "fromEmail": "abb@gmail.ca",
      "fromName": "Test"
    }
  </code></pre>
  
  <p>So I assume that in some way when the constructor of mycontroller is called, it's like if this line do all the job :</p>
  
  <p><code>_messageSender = messageSender;</code>
  By calling the contructor of <strong>MessageSender</strong> and using the parameter <strong>IOptions &lt; MessageSenderOptions ></strong> from the <strong>appsettings.json</strong> file.</p>
  
  <p>But I don't know how to use that in another class:</p>
  
  <pre><code> public class HelloJob : IJob
      {
          /*private readonly ILogger _logger;
          */
          private MessageSender _messageSender;
  
          _messageSender.SendEmailAsync("test@gmail.ca", subjet, message);
  
  }
  
  </code></pre>
  
  <p>How can I instantiate the <strong>MessageSender</strong> using the parameters of <strong>appsettings.json</strong> used in <strong>Startup.cs</strong> to call the method <strong>SendEmailAsync</strong></p>
  
  <p>Thank you for your help</p>