Monday, 29 August 2016

How to Send Mail in MVC6 Using C# Code With Attachment

8/29/2016 - By Pranav Singh 0


This article will show you how you can send an email using gmail as smtp server in mvc5 and mvc6 using c#.net with attachment. This application we can use in MVC3, MVC4, MVC5, MVC6.


Now we will create an image folder


So for this article first we will create a new  mvc application and add a controller and create the view for creating the form. This form will contain fields to provide information to send in email. Now add the below code in your view.

@{
    ViewBag.Title = "How to Send Mail in MVC6 Using C# Code With Attachment";
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <table width="100%" border="1">
        <tr>
            <td align="right" colspan="2" style="text-align: center"><strong>SEND EMAIL USING GMAIL ACCOUNT IN MVC</strong></td>
        </tr>
        <tr>
            <td align="right">&nbsp;</td>
            <td align="left">
                @ViewBag.Message
            </td>
        </tr>
        <tr>
            <td align="right">To :</td>
            <td align="left">
                <input id="txttoaddress" name="ToEmailId" width="250px" />
            </td>
        </tr>
        <tr>
            <td align="right">Subject :</td>
            <td align="left">
                <input id="txtsubject" name="Subject" width="250px"></input>
            </td>
        </tr>
        <tr>
            <td align="right">Message Body :</td>
            <td align="left">
                <textarea rows="4" cols="50" name="Message" id="txtmessage"></textarea>
            </td>
        </tr>
        <tr>
            <td align="right">Attachment :</td>
            <td align="left">
                <input type="file" name="file" />
            </td>
        </tr>

        <tr>
            <td align="center" colspan="2">
                <input type="submit" text="Send Message" />
            </td>
        </tr>
    </table>
}
In above code I have added some textbox and button control and input file control. Here we need to take care one thing about the name of the control. You must use a specific name of a control. So we use name of the control to access the value of control.

After this we will the below code to the controller.

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

namespace MvcApplication8.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string ToEmailId, string Subject, string Message, HttpPostedFileBase file)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(ToEmailId);
                mail.From = new MailAddress("dotnetpools@gmail.com");
                mail.Subject = Subject;
                string Body = Message;
                mail.Body = Body;
                mail.IsBodyHtml = true;

                //Send Attahment

                System.Net.Mail.Attachment attachment;
                string filename = System.IO.Path.GetFileName(file.FileName);
                /*Saving the file in server folder*/
                file.SaveAs(Server.MapPath("~/Images/" + filename));
                string filepathtoattach = "Images/" + filename;
                attachment = new System.Net.Mail.Attachment(Server.MapPath(filepathtoattach));
                mail.Attachments.Add(attachment);

                SmtpClient smtp = new SmtpClient();
                //SMTP Server Address of gmail
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("dotnetpools@gmail.com", "BlackBery1@#");
                // Smtp Email ID and Password For authentication
                smtp.EnableSsl = true;
                smtp.Send(mail);
                ViewBag.Message = "Message snd successfully";
            }
            catch
            {
                ViewBag.Message = "Error............";
            }

            return View();
        }
    }
}


So in above code I have used the smtp of gmail for sending the email.  In above please check the parameter passed in the httppost method.

In above code I have used System.Net.Mail.Attachment and then I find out the file name and then save it into the images folder. And then I passed the saved file path in mail attachment .
Keep it same as the control names in your view.Now one this after execution if you are getting error message as shown below:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

Then follow the below link to resolve the error.


Now we have done. Run the application to check the output.


Now check the received email




About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top