title: How to send an email on Postal using an email address from the database Asp.Net MVC
body: <p>I've currently implemented <a href="http://aboutcode.net/postal/" rel="nofollow">Postal</a> and I'm using that to send emails on my MVC application. The problem I'm having is that I want to retrieve an email address from my database, instead of hard-coding the address I want to send the email to. </p>
  
  <p>So to give a bit of context on where I'd like to implement this. After I create my Order, I want to send an email address to the Hospital that is linked to that order. </p>
  
  <p>This is what I have tried, but it throws an exception on this line <code>email.To = order.Hospital.Email;</code> - It does work if I hard code the email address like, for example , <code>email.To = "hospital@hospital.com".</code> </p>
  
  <blockquote>
    <p>An exception of type 'System.NullReferenceException' occurred in
    HealthHabitat.dll but was not handled in user code</p>
  </blockquote>
  
  <p>I've included it in my <code>Create POST method</code> so that it triggers once the model has been saved. </p>
  
  <p>The email address <strong>is</strong> saved as a <strong>string</strong> in my Hospital Model. </p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Create([Bind(Include = "OrderID,HospitalID,StaffID,Date,Time,Expected_Date")] Order order)
  {
      if (ModelState.IsValid)
      {
          order.StaffID = 5;
          db.Orders.Add(order);
          db.SaveChanges();
          dynamic email = new Email("Example");
          email.To = order.Hospital.Email; // this is where you set the email you are sending to. 
          email.Send();
          Success(string.Format("&lt;i class='fa fa-plus-circle'&gt;&lt;/i&gt; Please Add your Items!", order.OrderID), true);
          return RedirectToAction("Details", new { id = order.OrderID });
      }
  
  
      ViewBag.HospitalID = new SelectList(db.Hospitals, "HospitalID", "Name", order.HospitalID);
      Danger("&lt;i class='fa fa-warning'&gt;&lt;/i&gt; Error!", true);
      return View(order);
  }
  </code></pre>
  
  <p>Does any one have a solution to this? I'll greatly appreciate it and thanks in advance!</p>
  
  <p>If any additional information is needed, please let me know and I'll add it to the question. </p>
  
  <p><strong>Hospital Model:</strong></p>
  
  <pre><code>public class Hospital
  {
      public int HospitalID { get; set; }
  
      public string Name { get; set; }
  
      public string Province { get; set; }
  
      public string Email { get; set; }
  
      public string Phone_Number { get; set; }
  
  
      public virtual ICollection&lt;Order&gt; Orders { get; set; }
  }
  }
  </code></pre>