title: Trying to call code in my controller but getting Null Reference error
body: <p>Don't want to over-complicate the issue, but I think I need to post all the code that's hooked into this error.</p>
  
  <p>Using <a href="https://github.com/smsohan/MvcMailer" rel="nofollow">MvcMailer</a> and introduced a separate <code>Send</code> mechanism (for use with Orchard CMS' own EMail).</p>
  
  <p><strong>The MvcMailer Code:</strong></p>
  
  <p>1) AskUsMailer.cs:</p>
  
  <pre><code>public class AskUsMailer : MailerBase, IAskUsMailer
  {
      public AskUsMailer()
          : base()
      {
          //MasterName = "_Layout";
      }
  
      public virtual MvcMailMessage EMailAskUs(AskUsViewModel model)
      {
          var mailMessage = new MvcMailMessage { Subject = "Ask Us" };
          ViewData.Model = model;
          this.PopulateBody(mailMessage, viewName: "EMailAskUs");
          return mailMessage;
      }
  }
  </code></pre>
  
  <p>2) IAskUsMailer.cs:</p>
  
  <pre><code>public interface IAskUsMailer : IDependency
  {
      MvcMailMessage EMailAskUs(AskUsViewModel model);
  }
  </code></pre>
  
  <p>3) AskUsController.cs: <strong>(GETTING NULL REFERENCE ERROR BELOW)</strong></p>
  
  <pre><code>[Themed]
      public ActionResult Submitted()
      {
          //This is the new call (see new code below):
          //Note: Debugging steps through eMailMessagingService,
          //then shows the null reference error when continuing to
          //SendAskUs
          eMailMessagingService.SendAskUs(askUsData);
          //Below is normal MvcMailer call:
          //AskUsMailer.EMailAskUs(askUsData).Send();
          return View(askUsData);
      }
  </code></pre>
  
  <p>Note: askUsData is defined in a separate block in the controller:</p>
  
  <pre><code>    private AskUsViewModel askUsData;
      protected override void OnActionExecuting(ActionExecutingContext 
          filterContext)
      {
          var serialized = Request.Form["askUsData"];
          if (serialized != null) //Form was posted containing serialized data
          {
              askUsData = (AskUsViewModel)new MvcSerializer().
                  Deserialize(serialized, SerializationMode.Signed);
              TryUpdateModel(askUsData);
          }
          else
              askUsData = (AskUsViewModel)TempData["askUsData"] ?? 
                  new AskUsViewModel();
          TempData.Keep();
      }
      protected override void OnResultExecuted(ResultExecutedContext 
          filterContext)
      {
          if (filterContext.Result is RedirectToRouteResult)
              TempData["askUsData"] = askUsData;
      }
  </code></pre>
  
  <p>I did not know how to get my EMailMessagingService.cs (see below) call into the controller, so in a separate block in the controller I did this:</p>
  
  <pre><code>    private IEMailMessagingService eMailMessagingService;
  
      public AskUsController(IEMailMessagingService eMailMessagingService)
      {
          this.eMailMessagingService = eMailMessagingService;
      }
  </code></pre>
  
  <p>I think this is part of my problem.</p>
  
  <p><strong>Now, the new code trying to hook into Orchard's EMail:</strong></p>
  
  <p>1) EMailMessagingServices.cs:</p>
  
  <pre><code>public class EMailMessagingService : IMessageManager
  {
      private IAskUsMailer askUsMailer;
      private IOrchardServices orchardServices;
      public EMailMessagingService(IAskUsMailer askUsMailer, 
          IOrchardServices orchardServices)
      {
          this.orchardServices = orchardServices;
          this.askUsMailer = askUsMailer;
          this.Logger = NullLogger.Instance;
      }
  
      public ILogger Logger { get; set; }
  
      public void SendAskUs(AskUsViewModel model)
      {
          var messageAskUs = this.askUsMailer.EMailAskUs(model);
          messageAskUs.To.Add("email@email.com");
          //Don't need the following (setting up e-mails to send a copy anyway)
          //messageAskUs.Bcc.Add(AdminEmail);
          //messageAskUs.Subject = "blabla";
  
          Send(messageAskUs);
      }
      ....
  }
  </code></pre>
  
  <p>The EMailMessagingService.cs also contains the Send method:</p>
  
  <pre><code>    private void Send(MailMessage messageAskUs)
      {
          var smtpSettings = orchardServices.WorkContext.
              CurrentSite.As&lt;SmtpSettingsPart&gt;();
  
          // can't process emails if the Smtp settings have not yet been set
          if (smtpSettings == null || !smtpSettings.IsValid())
          {
              Logger.Error("The SMTP Settings have not been set up.");
              return;
          }
  
          using (var smtpClient = new SmtpClient(smtpSettings.Host, 
              smtpSettings.Port))
          {
              smtpClient.UseDefaultCredentials = 
                  !smtpSettings.RequireCredentials;
              if (!smtpClient.UseDefaultCredentials &amp;&amp;
                  !String.IsNullOrWhiteSpace(smtpSettings.UserName))
              {
                  smtpClient.Credentials = new NetworkCredential
                      (smtpSettings.UserName, smtpSettings.Password);
              }
  
              if (messageAskUs.To.Count == 0)
              {
                  Logger.Error("Recipient is missing an email address");
                  return;
              }
  
              smtpClient.EnableSsl = smtpSettings.EnableSsl;
              smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
  
              messageAskUs.From = new MailAddress(smtpSettings.Address);
              messageAskUs.IsBodyHtml = messageAskUs.Body != null &amp;&amp;
                  messageAskUs.Body.Contains("&lt;") &amp;&amp; 
                  messageAskUs.Body.Contains("&gt;");
  
              try
              {
                  smtpClient.Send(messageAskUs);
                  Logger.Debug("Message sent to {0} with subject: {1}",
                     messageAskUs.To[0].Address, messageAskUs.Subject);
              }
              catch (Exception e)
              {
                  Logger.Error(e, "An unexpected error while sending 
                      a message to {0} with subject: {1}", 
                      messageAskUs.To[0].Address, messageAskUs.Subject);
              }
          }
      }
  </code></pre>
  
  <p>Now, in EMailMessagingService.cs I was getting an error that things weren't being implemented, so I auto-generated the following (don't know if this is part of my error):</p>
  
  <pre><code>    public void Send(Orchard.ContentManagement.Records.ContentItemRecord recipient, string type, string service, System.Collections.Generic.Dictionary&lt;string, string&gt; properties = null)
      {
          throw new NotImplementedException();
      }
  
      public void Send(System.Collections.Generic.IEnumerable&lt;Orchard.ContentManagement.Records.ContentItemRecord&gt; recipients, string type, string service, System.Collections.Generic.Dictionary&lt;string, string&gt; properties = null)
      {
          throw new NotImplementedException();
      }
  
      public void Send(System.Collections.Generic.IEnumerable&lt;string&gt; recipientAddresses, string type, string service, System.Collections.Generic.Dictionary&lt;string, string&gt; properties = null)
      {
          throw new NotImplementedException();
      }
  
      public bool HasChannels()
      {
          throw new NotImplementedException();
      }
  
      public System.Collections.Generic.IEnumerable&lt;string&gt; GetAvailableChannelServices()
      {
          throw new NotImplementedException();
      }
  </code></pre>
  
  <p>2) IEMailMessagingServices.cs</p>
  
  <pre><code>public interface IEMailMessagingService
  {
      MailMessage SendAskUs(AskUsViewModel model);
  }
  </code></pre>
  
  <p>MvcMailer works fine without this addition (outside of Orchard), but I am trying to get everything working within Orchard.</p>
  
  <p>I just cannot figure out what I am doing wrong. Any thoughts?</p>
  
  <p>Sorry for excessive code.</p>