Send Mail Using Asp.Net MVC

We know most of the client’s web application have ‘contact us’ page and and with the help of that page a client get all the messages, queries and enquiries, so we have to write a server side code to send all user requests to client’s inbox, also after request submisson, a mail send to user for the succesfull submission.

I am going to tell you how to send maill to both client and user with one submission using asp.net mvc.

Now, Open Visual Studio 15
“File” -> “New” -> “Project…”
Choose Visual C#- Web then select ASP.NET MVC4 Web Application

Step 1:Create project in your visual studio 15 & name it SendMail

Step 2:Select Empty template and choose MVC in below references and click ok.

Step 3:You can see in your project there are several folders and files like model, view and controller etc.

Step 4:Now right on models folder and extend the add menu and select class and name it Contact.cs

Step 5:Go to model folder and open Contact.cs file and write the model properties you need for conatct us page.

Step 6:Similiary add another class name Mail to your models folder and write properties for mail send.

Step 7:Right click controller folder and add new controller choose MVC 5 Controller-Empty and name it HomeController

 

Step 8:This is your controller class, change your action method index to Contact

Step 9: Right click on the action method ‘Contact()’ and Add View and in Template dropdown select ‘Create’ and and select your model class ie Conact.cs in Model class dropdown and click add.

Step 10: This is your contact view pageview page

Step 11: Add new folder to your project ‘mycode’ and add a class named ‘Data’ and paste the below code inside it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;
using System.Web.Configuration;
using System.Net.Mail;
using System.Net;
namespace SendMail.mycode
{
public class Data
{
public static bool SendMailMaster(string email, string subject, string body, out string msg)
{
bool res = false;
msg = “”;
dynamic txtEmail = “enquiry@clientemailid.com”;
MailMessage mm = new MailMessage(txtEmail, email);
//mm.Bcc.Add(adminmail);
dynamic txtPassword = “enquiry@clientemailid”;
//mm.CC.Add(cc);

mm.Subject = subject;
mm.Body = body;
mm.BodyEncoding = System.Text.Encoding.GetEncoding(“utf-8”);
mm.SubjectEncoding = System.Text.Encoding.GetEncoding(“utf-8”);
mm.IsBodyHtml = true;
mm.Bcc.Add(txtEmail);
SmtpClient smtp = new SmtpClient();
smtp.Host = “relay-hosting.secureserver.net”;
smtp.EnableSsl = false;

NetworkCredential NetworkCred = new
System.Net.NetworkCredential(txtEmail, txtPassword);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 25;
try
{
smtp.Send(mm);
res = true;
}

catch (Exception ex)
{
msg = ex.ToString();
}
return res;
}

public static string contactusadmin(SendMail.Models.Contact bn)
{
string data = “<!DOCTYPE html><meta content=’width=device-width’name=viewport><meta content=’text/html; charset=UTF-8’http-equiv=Content-Type><title>clientemailid.com</title><style>@media only screen and (max-width:620px){table[class=body] h1{font-size:28px!important;margin-bottom:10px!important}table[class=body] a,table[class=body] ol,table[class=body] p,table[class=body] span,table[class=body] td,table[class=body] ul{font-size:16px!important}table[class=body] .article,table[class=body] .wrapper{padding:10px!important}table[class=body] .content{padding:0!important}table[class=body] .container{padding:0!important;width:100%!important}table[class=body] .main{border-left-width:0!important;border-radius:0!important;border-right-width:0!important}table[class=body] .btn table{width:100%!important}table[class=body] .btn a{width:100%!important}table[class=body] .img-responsive{height:auto!important;max-width:100%!important;width:auto!important}}@media all{.ExternalClass{width:100%}.ExternalClass,.ExternalClass div,.ExternalClass font,.ExternalClass p,.ExternalClass span,.ExternalClass td{line-height:100%}.apple-link a{color:inherit!important;font-family:inherit!important;font-size:inherit!important;font-weight:inherit!important;line-height:inherit!important;text-decoration:none!important}.btn-primary table td:hover{background-color:#34495e!important}.btn-primary a:hover{background-color:#34495e!important;border-color:#34495e!important}}</style><body style=background-color:#f6f6f6;font-family:sans-serif;-webkit-font-smoothing:antialiased;font-size:14px;line-height:1.4;margin:0;padding:0;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%><table style=border-collapse:separate;mso-table-lspace:0;mso-table-rspace:0;width:100%;background-color:#f6f6f6 border=0 cellpadding=0 cellspacing=0 class=body><tr><td style=font-family:sans-serif;font-size:14px;vertical-align:top><td style=’font-family:sans-serif;font-size:14px;vertical-align:top;display:block;Margin:0 auto;max-width:580px;padding:10px;width:580px’class=container><div class=content style=’box-sizing:border-box;display:block;Margin:0 auto;max-width:580px;padding:10px’><span class=preheader style=color:transparent;display:none;height:0;max-height:0;max-width:0;opacity:0;overflow:hidden;mso-hide:all;visibility:hidden;width:0>Mail from clientemailid.</span><table style=border-collapse:separate;mso-table-lspace:0;mso-table-rspace:0;width:100%;background:#fff;border-radius:3px class=main><tr><td style=font-family:sans-serif;font-size:14px;vertical-align:top;box-sizing:border-box;padding:20px class=wrapper><table style=border-collapse:separate;mso-table-lspace:0;mso-table-rspace:0;width:100% border=0 cellpadding=0 cellspacing=0><tr><td style=font-family:sans-serif;font-size:14px;vertical-align:top><p style=font-family:sans-serif;font-size:14px;font-weight:400;margin:0;Margin-bottom:15px>Hi Sir,<p style=font-family:sans-serif;font-size:14px;font-weight:400;margin:0;Margin-bottom:15px>A user recently query on your website , below are the details of user. Thanks!<table style=border-collapse:separate;mso-table-lspace:0;mso-table-rspace:0;width:100%;box-sizing:border-box border=0 cellpadding=0 cellspacing=0 class=’btn btn-primary’><tr><td>Name : ” + bn.Name + “<td>Email : ” + bn.Email + “<td>Mobile : ” + bn.Mobile + “<td>Message : ” + bn.Message + “</table><p style=font-family:sans-serif;font-size:14px;font-weight:400;margin:0;Margin-bottom:15px>Have a good day !</table></table><div class=footer style=clear:both;Margin-top:10px;text-align:center;width:100%><table style=border-collapse:separate;mso-table-lspace:0;mso-table-rspace:0;width:100% border=0 cellpadding=0 cellspacing=0><tr><td style=font-family:sans-serif;vertical-align:top;padding-bottom:10px;padding-top:10px;font-size:12px;color:#999;text-align:center class=’content-block powered-by’>Send by <a href=http://clientemailid.com style=color:#999;font-size:12px;text-align:center;text-decoration:none>clientemailid.com</a>.</table></div></div><td style=font-family:sans-serif;font-size:14px;vertical-align:top></table>”;
return data;
}
public static string contactususer(SendMail.Models.Contact bn)
{
return “”;
}

}
}

Step 12: Add below code in your controller class:
public ActionResult Contact()
{
return View();
}
[HttpPost]
public ActionResult Contact(SendMail.Models.Conatct c)
{ if (ModelState.IsValid)
{
mycode.Data ob = new mycode.Data();

TempData[“data”] = “Message Has Been Send Successfully…..!!”;
string msg = “”;

bool issendtoadmin = mycode.Data.SendMailMaster(“enquiry@clientemailid.com”, “New Enquiry”, “new enuiry from user here are the details <br> Name : ” + c.Name + ” <br> Email : ” + c.Email + ” <br> Mobile : ” + c.Mobile + ” <br> Message : ” + c.Message + “”, out msg);

bool issendtouser = mycode.Data.SendMailMaster(c.Email, “Confirmation”, mycode.Data.contactususer(c), out msg);

return View(new SendMail.Models.Contact());

}
else
{
return View(c);
}
}