python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
send email using mvc 4

how to send email using mvc 4


Hi, I am going to share the code sample for sending email in MVC 4 using C#, $.getJSON() and jQuery.

Table of Contents

1. Code sample for appSetting config file
2. Code sample for MVC 4 Controller.cs file
3. Code sample for MVC 4 cshtml view page
4. Code sample for jQuery with $.getJSON() method

appSetting config file

    <!-- EMail Setting-->
    <add key="ToEmail" value="[email protected]"/>
    <add key="UserID" value="[email protected]"/>
    <add key="Password" value="password"/>
    <add key="SMTPPort" value="587"/>
    <add key="Host" value=""/>
    <!-- End EMail Setting--> 

MVC 4 Controller.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;

namespace PCX.Controllers
    public class MailSendController : Controller
        public ActionResult Index()
            return View();

        public JsonResult getSendEmail(string From, string Subject, string Body)
            string To;
            string UserID;
            string Password;
            string SMTPPort;
            string Host;

            //Get the AppSettings.
            AppSettings(out To, out UserID, out Password, out SMTPPort, out Host);

            //Call send email methods.
            Send(From, Subject, Body, To, UserID, Password, SMTPPort, Host);
            ViewBag.Sent = true;
            //return RedirectToAction("Supports", "Account");
            return Json("Success");

        //This is used for ConfigurationManager EMail Setting.
        private static void AppSettings(out string To, out string UserID, out string Password, out string SMTPPort, out string Host)
            To = System.Configuration.ConfigurationManager.AppSettings.Get("ToEmail");
            UserID = System.Configuration.ConfigurationManager.AppSettings.Get("UserID");
            Password = System.Configuration.ConfigurationManager.AppSettings.Get("Password");
            SMTPPort = System.Configuration.ConfigurationManager.AppSettings.Get("SMTPPort");
            Host = System.Configuration.ConfigurationManager.AppSettings.Get("Host");

        private static void Send(string From, string Subject, string Body, string To, string UserID, string Password, string SMTPPort, string Host)
                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                mail.From = new MailAddress(From);
                mail.Subject = Subject;
                mail.Body = Body;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = Host;
                smtp.Port = Convert.ToInt16(SMTPPort);
                smtp.Credentials = new System.Net.NetworkCredential(UserID, Password);
                smtp.EnableSsl = true;
            catch (Exception ex)
                //Log enternal eror in db.

MVC 4 cshtml view page

    MvcApplication1.Models.UserSession userSession = (MvcApplication1.Models.UserSession)Session["userSession"];

<div class="panel-body">
    <div class="row">
        <div class="form-horizontal form-widgets col-sm-8">
            <div id="clientsDb">
                <div id="forms" data-role="view" data-title="Form Elements" data-init="initForm" data-use-native-scrolling="true">
                        <div class="form-group">
                            <label class="col-sm-3">
                            <div class="col-sm-8">
                                <input type="text" id="txtSubject" class="k-textbox">
                        <div class="form-group" data-role="listview" data-style="inset">
                            <label class="col-sm-3">
                                Add Comment
                            <div class="col-sm-8">
                                <textarea class="form-control" id="txtBody" rows="10"></textarea>
                        <div class="col-sm-11">
                            <a class="btn btn-primary pull-right" id="btnSendComment">Send Comment</a>
        <div class="col-sm-4">
            <img src="~/Images/support_image.png" />

jQuery with $.getJSON() method

<script type="text/javascript">
    $(document).ready(function () {
        $('#btnSendComment').click(function () {
    function validate(subject, body) {
        var isValid = true;
        if (subject !== undefined) {
            if (subject.length <= 0) {
                showMSG($("#msg"), 'Please enter subject.');
                isValid = false;
                return false;

        if (body != undefined) {
            if (body.length <= 0) {
                showMSG($("#msg"), 'Please enter comment.');
                isValid = false;
        return isValid;

    function getEmailSupportData() {
        var subject = $('#txtSubject').val();
        var body = $('#txtBody').val();
        var isValid = validate(subject, body);
        if (isValid) {
            var fromEmail = '@userSession.EmailID';
            var url = '~/MailSend/getSendEmail';
            sendEmail(url, subject, body, fromEmail);

    function sendEmail(url, subject, body, fromEmail) {
        $.getJSON(url, { From: fromEmail, Subject: subject, Body: body })
        .done(function (data) {
            if (data !== undefined && data !== null) {
                showMSG($("#msg"), 'Your email has been sent successfully');


Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like Powered by Blogger.
ASK Questions