Sunday, 18 September 2016

How to send email from MVC 5 application-Mozammal Farhad

Do the following step:

1.
Make a new MVC project name

 Send_Email_MVC

then make a model class in model folder in your mvc project.

public class EmailModel
    {
        public string To { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public HttpPostedFileBase Attachment { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
    }


2.

Second make a controller name     Home   in controller folder in your mvc project




using Send_Email_MVC.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 Send_Email_MVC.Controllers
{

[HttpPost]
        public ActionResult Index(EmailModel model)
        {
            using (MailMessage mm = new MailMessage(model.Email, model.To))
            {
                mm.Subject = model.Subject;
                mm.Body = model.Body;
                if (model.Attachment.ContentLength > 0)
                {
                    string fileName = Path.GetFileName(model.Attachment.FileName);
                    mm.Attachments.Add(new Attachment(model.Attachment.InputStream, fileName));
                }
                mm.IsBodyHtml = false;
                using (SmtpClient smtp = new SmtpClient())
                {
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    NetworkCredential NetworkCred = new NetworkCredential(model.Email, model.Password);
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = NetworkCred;
                    smtp.Port = 587;
                    smtp.Send(mm);
                    ViewBag.Message = "Email sent.";
                }
            }

            return View();
        }
}




3.



Finally make a view by right clicking on your controller Action Method




the view look like this



@model Send_Email_MVC.Models.EmailModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        }

        table th, table td {
            padding: 5px;
        }
    </style>
</head>
<body>
    <div>
        @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td style="width: 80px">
                        To:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.To)
                    </td>
                </tr>
                <tr>
                    <td>
                        Subject:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.Subject)
                    </td>
                </tr>
                <tr>
                    <td valign="top">
                        Body:
                    </td>
                    <td>
                        @Html.TextAreaFor(model => model.Body, new { rows = "3", cols = "20" })
                    </td>
                </tr>
                <tr>
                    <td>
                        File Attachment:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.Attachment, new { type = "file" })
                    </td>
                </tr>
                <tr>
                    <td>
                        Gmail Email:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.Email)
                    </td>
                </tr>
                <tr>
                    <td>
                        Gmail Password:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.Password, new { type = "password" })
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" value="Send" />
                    </td>
                </tr>
            </table>
            <br />
            <span style="color:green">@ViewBag.Message</span>
        }
    </div>
</body>

</html>





Now run the project 

you can see the view in ur browser by filling up the data then click sent


Comments system

Advertising

Disqus Shortname