Send Email Attachment MVC 5 ASP.Net

How Send Email with Attached file in ASP.NET MVC 5?

Table of Contents –
1.     Create a new Model Class in the model
2.     Create a new Home Controller in the Controller
3.     Create a new Email Action with HttpPost method in the Home Controller
4.     Create a HTML UX Page
5.     Return Email Sent msg -Email has been sent successfully!
6.     Result
Stayed Informed MVC Interview Q/A

Steps 
Create a new Email Model Class in the model -

namespace SendEmailwithAttchedFile.Controllers
{
    public class EmailModel
    {
        public string To { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
    }
}


Create a new Home Controller in the Controller and an Email Action with HttpPost method in the Home Controller –
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;

namespace SendEmailwithAttchedFile.Controllers
{
    public class HomeController : Controller
    {
        [HttpPost]
        [ActionName("Email")]
        public ActionResult SendAttachEmail(EmailModel objModelMail, HttpPostedFileBase Attachedfile)
        {
            if (ModelState.IsValid)
            {
                string from = "youremail@gmail.com"; //Email like- anil.singh581@gmail.com
                using (MailMessage mail = new MailMessage(from, objModelMail.To))
                {
                    mail.Subject = objModelMail.Subject;
                    mail.Body = objModelMail.Body;
                    if (Attachedfile != null)
                    {
                        string fileName = Path.GetFileName(Attachedfile.FileName);
                        mail.Attachments.Add(new Attachment(Attachedfile.InputStream, fileName));
                    }
                    mail.IsBodyHtml = false;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    NetworkCredential networkCredential = new NetworkCredential(from, "your@Password");
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = networkCredential;
                    smtp.Port = 587;
                    smtp.Send(mail);

                    ViewBag.Message = "EmailSent";

                    return View("Index", objModelMail);
                }
            }
            else
            {
                return View();
            }
        }
    }   
}


Create a HTML UX Page –
@model SendEmailwithAttchedFile.Controllers.EmailModel
@{
    ViewBag.Title = "Index";
}
<h3>Send Email With Attached File</h3>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>

@using (@Html.BeginForm("Email", "Home", FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary()
    <div>
        <table>
            <tbody>
                <tr>
                    <td>To:</td>
                    <td>@Html.TextBoxFor(m => m.To)</td>
                </tr>
                <tr>
                    <td>Subject:</td>
                    <td>@Html.TextBoxFor(m => m.Subject)</td>
                </tr>
                <tr>
                    <td>Attachment</td>
                    <td><input name="Attachedfile" type="file" /></td>
                </tr>
                <tr>
                    <td>Body:</td>
                    <td>@Html.TextAreaFor(m => m.Body)</td>
                </tr>
            </tbody>
        </table>
    </div>
    <div><input type="submit" value="Send" /></div>
}

<script type="text/javascript">
$(function () {
    if ('@ViewBag.Message' === 'EmailSent') {
        alert('Email has been sent successfully!');
    }
});
</script>


Return Email Sent msg -Email has been sent successfully! And the Result looks like-



I hope you are enjoying with this post! Please share with you friends. Thank you!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^