title: Attachment with Postal MVC don't work
body: <p>I have a <code>MVC3 project</code>. In my contact page I would like to add the possibility of adding an attachment to Email sent by customer.</p>
  
  <p>I am using <code>Postal</code>. It works well but I can't get the attachment working.</p>
  
  <p>Here is the part of my code dealing with this:</p>
  
  <p>I have a <code>model class</code>:</p>
  
  <pre><code>public class ContactEmail
  {
      [ScaffoldColumn(false)]
      public int id { get; set; }
  
      [Required(ErrorMessage = "Name is required!")]
      [DataType(DataType.Text)]
      [DisplayName("Name")]
      public string name { get; set; }
  
      [Required(ErrorMessage = "Email Address is required!")]
      [DataType(DataType.EmailAddress)]
      [DisplayName("Email Address")]
      [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage = "Email Address is not valid.")]
      public string email { get; set; }
  
      [Required(ErrorMessage = "Country is required!")]
      [DisplayName("Country")]
      public string country { get; set; }
  
      [DisplayName("Subject")]
      public string subject { get; set; }
  
      [Required(ErrorMessage = "Message is required!")]
      [DisplayName("Message")]
      [StringLength(1500)]
      public string message { get; set; }
  
      [ScaffoldColumn(false)]
      public DateTime datetime { get; set; }
  
      public HttpPostedFileBase attachment { get; set; }
  </code></pre>
  
  <p>Here is my <code>Postal Controller</code>:</p>
  
  <pre><code>public class PostalController : Controller
  {
      [HttpPost]
      public ActionResult Send(ContactEmail model)
      {
  
          if (ModelState.IsValid)
          {
              Random random = new Random();
              var ticket = random.Next(1000000000, 2000000000);
  
              dynamic courriel = new Email("Postal");
              courriel.To = "support@coderfortraders.com";
              courriel.Name = model.name;
              courriel.Country = model.country;
              courriel.Subject = model.subject;
              courriel.From = model.email;
              courriel.Message = model.message;
              courriel.TicketId = ticket;
              courriel.Attachment = model.attachment;
              //if (model.Attachment != null &amp;&amp; model.Attachment.ContentLength &gt; 0)
              //{
              //    var attachment = new Attachment(model.Attachment.InputStream, model.Attachment.FileName);
              //    courriel.Attachments.Add(attachment);
              //}
              //courriel.Attachment = model.Attachment; 
  
              courriel.Attach(new Attachment(model.attachment.InputStream, model.attachment.FileName));
  
              Task task = courriel.SendAsync();
  
              //courriel.Send();
  
              return RedirectToAction("Sent", "Contact");
          }
  
          ViewData["MessageSent"] = false;
  
          return View("~/Views/Contact/Contact.cshtml");
      }
  </code></pre>
  
  <p>and finally here is the form I use in my <code>Contact.cshtml view</code>:</p>
  
  <pre><code>@using (Html.BeginForm("Send", "Postal"))
  {        
      @Html.ValidationSummary(false)
  
      &lt;p&gt;
         @Html.LabelFor(model =&gt; model.name)
         @Html.TextBoxFor(model =&gt; model.name, new { @size = 35 })
         @Html.ValidationMessageFor(model =&gt; model.name, "*")
      &lt;/p&gt;                      
      &lt;p&gt;
         @Html.LabelFor(model =&gt; model.email)
         @Html.TextBoxFor(model =&gt; model.email, new { @size = 35 })
         @Html.ValidationMessageFor(model =&gt; model.email, "*")
      &lt;/p&gt;
      &lt;p&gt;
         @Html.LabelFor(model =&gt; model.subject)
         @Html.DropDownList(
             "subject", 
             new[] { 
                 new SelectListItem { 
                     Text = "NT Indicator Quote", 
                     Value = "NT Indicator Quote" 
                 }, 
                 new SelectListItem { 
                     Text = "NT Strategy Quote", 
                     Value = "NT Strategy Quote" 
                 }, 
                 new SelectListItem { 
                     Text = "Comments", 
                     Value = "Comments" 
                 }, 
                 new SelectListItem { 
                     Text = "other", 
                     Value = "other" 
                 }
             }
         ); // @Html.DropDownList(
         @Html.ValidationMessageFor(model =&gt; model.subject, "*")
      &lt;/p&gt;
      &lt;p&gt;
         @Html.LabelFor(model =&gt; model.message)
         @Html.TextAreaFor(model =&gt; model.message, new { @cols = 50, @rows = 10 })
         @Html.ValidationMessageFor(model =&gt; model.message, "*")
      &lt;/p&gt;
      &lt;p&gt;
         @Html.LabelFor(model =&gt; model.attachment)
         @Html.TextBoxFor(model =&gt; model.attachment, new { type = "file" })
         @Html.ValidationMessageFor(model =&gt; model.attachment)
      &lt;/p&gt;          
      &lt;p&gt;&lt;input type="submit" id="submit" name="submit" value="Submit" /&gt;&lt;/p&gt;            
  } &lt;!-- End form --&gt;
  </code></pre>
  
  <p>I get the following error message :</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.<br>
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.<br>
    Source Error: 
    Line 44: courriel.Attach(new Attachment(model.attachment.InputStream, model.attachment.FileName));</p>
  </blockquote>
  
  <p>Could you please help me find out the problem here ?
  Thank you !</p>
  
  <p>Here is the HTML from the view source :</p>
  
  <pre><code>&lt;form action="/Postal/Send?enctype=multipart%2Fform-data" method="post"&gt;
      &lt;div class="validation-summary-valid" data-valmsg-summary="true"&gt;
          &lt;ul&gt;
              &lt;li style="display:none"&gt;&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/div&gt;
      &lt;p&gt;
          &lt;label for="name"&gt;Name&lt;/label&gt;
          &lt;input data-val="true" data-val-required="Name is required!" id="name" name="name" size="35" type="text" value="" /&gt;
          &lt;span class="field-validation-valid" data-valmsg-for="name" data-valmsg-replace="false"&gt;*&lt;/span&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label for="email"&gt;Email Address&lt;/label&gt;
          &lt;input data-val="true" data-val-regex="Email Address is not valid." data-val-regex-pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}" data-val-required="Email Address is required!" id="email" name="email" size="35" type="text" value="" /&gt;
          &lt;span class="field-validation-valid" data-valmsg-for="email" data-valmsg-replace="false"&gt;*&lt;/span&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label for="subject"&gt;Subject&lt;/label&gt;
          &lt;select id="subject" name="subject"&gt;
              &lt;option value="NT Indicator Quote"&gt;NT Indicator Quote&lt;/option&gt;
              &lt;option value="NT Strategy Quote"&gt;NT Strategy Quote&lt;/option&gt;
              &lt;option value="Comments"&gt;Comments&lt;/option&gt;
              &lt;option value="other"&gt;other&lt;/option&gt;
          &lt;/select&gt;
          &lt;span class="field-validation-valid" data-valmsg-for="subject" data-valmsg-replace="false"&gt;*&lt;/span&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label for="message"&gt;Message&lt;/label&gt;
          &lt;textarea cols="50" data-val="true" data-val-length="The field Message must be a string with a maximum length of 1500." data-val-length-max="1500" data-val-required="Message is required!" id="message" name="message" rows="10"&gt;
          &lt;/textarea&gt;
          &lt;span class="field-validation-valid" data-valmsg-for="message" data-valmsg-replace="false"&gt;*&lt;/span&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label for="attachment"&gt;attachment&lt;/label&gt;
          &lt;input id="attachment" name="attachment" type="file" value="" /&gt;
          &lt;span class="field-validation-valid" data-valmsg-for="attachment" data-valmsg-replace="true"&gt;&lt;/span&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;input type="submit" id="submit" name="submit" value="Submit" /&gt;
      &lt;/p&gt;
  &lt;/form&gt; &lt;!-- End form --&gt;
  
      &lt;/div&gt;
  </code></pre>