How to send email with attachment using Gmail SMTP from MVC application - ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

How to send email with attachment using Gmail SMTP from MVC application

In this article I am going to explain how to send email with attachment using Gmail SMTP from MVC application.

I want to send email with attachment using Gmail account.

Open the web.config file of project. Set key in Appsettings. After that mailsettings as shown below:
    <add key="Email" value=""/>
      <smtp deliveryMethod="Network">
        <network enableSsl="true" port="587" host="" userName="" password="password"/>

 Add Controller
Add empty controller to project. Create an ActionResult to send email.

Complete code of Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2017.Models;
using System.Security.Cryptography;
using System.Configuration;
using System.Net.Mail;
using System.Net;
using System.IO;
namespace mvcapplication2017.Controllers
    public class LoginController : Controller
        // GET: /Login/
        DemoEntities db = new DemoEntities();
        public ActionResult Index()
            return View();
        public ActionResult SendEmail()
            return View();
        public ActionResult SendEmail(string Name, string Subject,string EmailID, string message,HttpPostedFileBase file1)
            if (ModelState.IsValid)
                string Email = ConfigurationManager.AppSettings["Email"].ToString();
                string Emailto = EmailID;
                MailMessage mail = new MailMessage(Email, Emailto);
                mail.Subject = Subject;
                string mailmessage = "Name :"+ Name +"<br>"+ message;
                mail.Body = mailmessage;
                mail.IsBodyHtml = true;
                if (file1 != null && file1.ContentLength > 0) 
                    string fileName = string.Format("{0}/{1}", Server.MapPath("~/images/"), Path.GetFileName(file1.FileName));
                    mail.Attachments.Add(new Attachment(fileName));
                SmtpClient smtp = new SmtpClient();
                ViewBag.Message = "Email sent successfully";
            return View();

Add view:
Add new view for SendEmail Actionresult.

Source code of View:

    ViewBag.Title = "Send Email";
<script src=""></script>

<h2>Send Email</h2>
@using (Html.BeginForm("SendEmail", "Login", FormMethod.Post, new { enctype = "multipart/form-data" }))
    <legend>Send Email</legend>
            <td>Name :</td>
            <td>@Html.TextBox("Name", "", new { @data_val = "true", @data_val_required = "Please enter name",placeholder="Name"})</td>
            <td>Email :</td>
            <td>@Html.TextBox("EmailID","", new { @data_val = "true",@data_val_email="email dsfdsfsdf", @data_val_required = "Please enter email address",placeholder="Email" })</td>
            <td>Subject :</td>
            <td>@Html.TextBox("Subject", "", new { @data_val = "true", @data_val_required = "Please enter subject", placeholder = "Subject" })</td>
            <td>Message :</td>
            <td>@Html.TextArea("message", "", new { rows = "6", cols = "22", @data_val = "true", @data_val_required = "Please enter message", placeholder = "Message" })</td>
            <td>Attachment :</td>
            <td><input id="File1" name="File1" type="file" /></td>
            <td><input type="submit" value="Send Email" /></td>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.