Career form with attachment via email in MVC

When we create any website then we need to always create a career form so here we learn about how to create a career form with attachment file. As we previously done Career form with attachment via email in asp.net using c#.

First we create Model with name (MailModel.cs)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCODESOLUTIONS.Models
{
    public class MailModel
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Subject { get; set; }
        public string Message { get; set; }
        public string Contact { get; set; }
  
    }
}
Controller
using MVCCODESOLUTIONS.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
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 e-mail address.
            Msg.From = new MailAddress(model.Email);
            //Recipient e-mail address.
            Msg.To.Add("sender@gmail.com");
            //Message Subject
            Msg.Subject = model.Subject;
            //Message body

            StringBuilder sb = new StringBuilder();
            sb.Append("Name :" + model.Name+ "\r\n");
            sb.Append("Contact:" + model.Contact + "\r\n");
            sb.Append("Email:" + model.Email + "\r\n");
            sb.Append("Message:" + model.Message + "\r\n");

            Msg.Body = sb.ToString();
            //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 = "Thanks for posting your resume us,our team will be contact you as soon as possible";


            return View();
        }
    }
}

View
@model MVCCODESOLUTIONS.Models.MailModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Career form with attachment via email </title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <fieldset style="width: 500px;">
            <legend>Career Form</legend>
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td>Name</td>
                    <td>@Html.TextBoxFor(m => m.Name)</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Contact Number:</td>
                    <td>@Html.TextBoxFor(m => m.Contact)</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td style="width: 80px">Email:</td>
                    <td>@Html.TextBoxFor(m => m.Email)</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.Message, new { @rows = 10, @cols = 50 })</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Resume 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:-
Career form with attachment via email in MVC Career form with attachment via email in MVC Reviewed by NEERAJ SRIVASTAVA on 8:29:00 PM Rating: 5

No comments:

Powered by Blogger.