Send email in MVC

As we learn previously  how to send mail yourself means admin .Now we learn how to send a mail to Recipient email. Many times we need to create an email system, Here we will use for Gmail but  you can use according your requirement you can use just change

Host – SMTP Server URL (Gmail: smtp.gmail.com).
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True).
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails.
Credentials – Valid login credentials for the SMTP server ( email ID and password).
Port – Port Number of the SMTP server (Gmail: 587)
Model (MailModel.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCODESOLUTIONS.Models
{
    public class MailModel
    {
        public string To { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
  

    }
}
Controller(HomeController)
using MVCCODESOLUTIONS.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;

namespace MVCCODESOLUTIONS.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }



        [HttpPost]
        public ActionResult Index(MailModel model, List<HttpPostedFileBase> attachments)
        {

            MailMessage Msg = new MailMessage("sender@gmail.com", model.To);
      
            //Message Subject
            Msg.Subject = model.Subject;
            //Message body
            Msg.Body = model.Body;
            //message attachment
            foreach (HttpPostedFileBase attachment in attachments)
            {
                if (attachment != null)
                {
                    string fileName = Path.GetFileName(attachment.FileName);
                    Msg.Attachments.Add(new Attachment(attachment.InputStream, fileName));
                }
            }
            Msg.IsBodyHtml = false;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "sender-password");
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(Msg);
            ViewBag.Message = "Email successfully sent";
            return View();
        }
    }
}

View (Index.cs.html)
@model MVCCODESOLUTIONS.Models.MailModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Send email in MVC</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <fieldset style="width: 500px;">
            <legend>Email Form</legend>
            <table border="0" cellpadding="0" cellspacing="0">
             
                <tr>
                    <td style="width: 180px">Recipient Email:</td>

                    <td>@Html.TextBoxFor(m => m.To)</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Subject:</td>
                    <td>@Html.TextBoxFor(m => m.Subject)</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td valign="top">Message:</td>
                    <td>@Html.TextAreaFor(m => m.Body, new { @rows = 10, @cols = 50 })</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>File Upload:</td>
                    <td><input type="file" name="Attachments" multiple="multiple" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
           
                <tr>
                    <td>&nbsp;</td>

                </tr>
                <tr>

                    <td></td>
                    <td><input type="submit" value="Send" /></td>
                </tr>
            </table>
        </fieldset>
    }
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        var message = "@ViewBag.Message";
        $(function () {
            if (message != "") {
                alert(message);
            }
        });
    </script>
</body>
</html>


Out-Put:-

Send email in MVC Send email in MVC Reviewed by NEERAJ SRIVASTAVA on 10:03:00 AM Rating: 5

No comments:

Powered by Blogger.