Sending Email With Attachment Using MVC4

Here i am explaining how to send email with attachment in mvc4
Design the page like below in view

@using (Html.BeginForm("Questionform", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBox("fname", "", new { id = "fname", placeholder = "first name", required = "required" })
@Html.TextBox("lname", "", new { id = "lname", placeholder = "last name", required = "required" })
@Html.TextBox("email", "", new { id = "email", placeholder = "email", required = "required", type = "email" })
@Html.TextBox("phone", "", new { id = "phone", @class = "numbers", placeholder = "phone", required = "required", maxlength = "10" })
@Html.TextArea("msg", "", new { id = "msg", placeholder = "message", required = "required" })
<h6>
<input type="file" name="file" id="file_browse" />Attach a file</h6>
@Html.Hidden("pageurl", Request.Url.AbsolutePath, new { id = "pageurl" })
<input type="submit" class="submit" value="send" id="btnquestionsend" />
}

In controller add below code

name-spaces:

using System.IO;
using System.Net.Mail;

Code:

public ActionResult Questionform(FormCollection collection, HttpPostedFileBase file)
{
string fname = collection.Get("fname");
string lname = collection.Get("lname");
string email = collection.Get("email");
string phone = collection.Get("phone");
string message = collection.Get("msg");
string pageurl = collection.Get("pageurl");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("venu.tamada@gmail.com");
msg.To.Add("applevenu@gmail.com");
msg.Subject = "amm@Questions?";
msg.IsBodyHtml = true;
msg.Body += "<div><table style=\"border-collapse:collapse; text-align:left;\">";
msg.Body += "<tr style =\"background-color:#6FA1D2; color:#ffffff;\"><td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">Name:</td><td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">" + fname + " " + lname + "</td></tr>";
msg.Body += "<tr style =\"background-color:#6FA1D2; color:#ffffff;\"><td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">Email:</td><td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">" + email + "</td></tr>";
msg.Body += "<tr style =\"background-color:#6FA1D2; color:#ffffff;\"><td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">Telephone:</td><td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">" + phone + "</td></tr>";
msg.Body += "<tr style =\"background-color:#6FA1D2; color:#ffffff;\"><td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">Message:</td><td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">" + message + "</td></tr>";
msg.Body += "</table></div>";
if (file != null && file.ContentLength > 0)
{
Attachment atc = new Attachment(file.InputStream, file.FileName);
msg.Attachments.Add(atc);
}
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("venu.tamada@gmail.com", "9#######");
smtp.EnableSsl = true;
smtp.Send(msg);
msg = null;
return Redirect(pageurl);
}

then run your project enjoy happy coding..