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.
Some of my previous
articles are as follows: Gmail
Smtp Error : 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, Read Outlook Inbox Mail And Email Count Of
Inbox Using C#.Net,
Business Email Validation Using
RegularExpressionValidator Control in Asp.net, Email Validation in Windows Application
C#.Net and VB.Net, Email Address Validation in Javascript in
Asp.Net, How to Add Email Id Hyperlink or Mailto Link
in Asp.net MVC WebGrid, Send Mail In Asp.Net C# Using Gmail.
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"> </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
0 comments:
Please let me know your view