title: How to send an email from an ASP.NET MVC view page with an attachment?
body: <p>I have to send an email from my ASP.NET MVC 2 contact form view page.I need a detail answer that describes how to create the model , the controller and the view for that purpose .. Here is the code i have given in my controller class's action method..</p>
  
  <pre class="lang-cs prettyprint-override"><code>[AcceptVerbs(HttpVerbs.Post)]
  public ActionResult SendEMail(CareersEMailModel careersEMailModel,HttpPostedFileBase upload)
  {
       if (ModelState.IsValid)
       {
              bool isOK = false;
  
              try
              {
                  MailMessage msg = new MailMessage();
                  msg.From = new MailAddress("no-reply@abc.com", "Website contact form");
                  msg.To.Add("info@abc.com");
                  msg.Subject = "Resume";
                  string body = "Name:" + careersEMailModel.Name + "\n" + "Phone:" + careersEMailModel.Phone + "\n" + "Email:" + careersEMailModel.Email;
                  string file = careersEMailModel.Resume;
                  msg.Body = body;
                  msg.IsBodyHtml = false;
                  SmtpClient smtp = new SmtpClient("mailserver_url.net", 25);
                  smtp.Send(msg);
                  msg.Dispose();
                  isOK = true;
                  CareersMessageModel rcpt = new CareersMessageModel();
                  rcpt.Title = "Email sent successfully!!";
                  rcpt.Content = "Your details has been received with great thanks.We'll contact you as soon as possible.";
                  return View("CareersMessage", rcpt);
              }
              catch (Exception ex)
              {
                  CareersMessageModel err = new CareersMessageModel();
                  err.Title = "Sorry,Email sending failed!!!";
                  err.Content = "The website is having an error with sending this mail at this time.You can send an email to our address provided in our contact us form.Thank you.";
                  return View("CareersMessage", err);
              }
          }
          else
          {
              return View();
          }
      }
  </code></pre>