ASP.NET MVC – Send Mails using Office365 Email SMTPClient

  • by

Hi Friends,

In this post we will see how we can send mails in ASP.NET MVC C# using Office365 email address and smtp client. We will create a MVC Contact us page with all the elements of a contact us form.

mvc-smtp-send-mail-office365

Create a Model class in your MVC Project with name Mail.cs and edit it as below:

Models > Mail.cs:

using System;

namespace Office365Mail.Models
{
public class Mail
{
public String Name { get; set; }
public String Message { get; set; }
public String Email { get; set; }
public String ContactNo { get; set; }
}
}

Create a new Controller with name ContactUsController.cs and edit it as below:

Controller>ContactUsController.cs:

using Office365Mail.Models;
using System;
using System.Net.Mail;
using System.Web.Mvc;

namespace Office365Mail.Controllers
{
public class ContactUsController : Controller
{
// GET: ContactUs
public ActionResult Index()
{
return View();
}

[HttpPost]
public ActionResult Index(Mail mail)
{
try
{
SmtpClient smtpClient = new SmtpClient("smtp.office365.com", 587);

smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials =
new System.Net.NetworkCredential("yourfrom@email.com", "emailfrompassword");
smtpClient.Port = 587;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;

MailMessage mailMessage = new MailMessage();

mailMessage.From = new MailAddress("yourfrom@email.com", "Website Enquiry");
mailMessage.To.Add(new MailAddress("yourTo@email.com", "Hitesh02"));
mailMessage.Subject = "Inquiry";
mailMessage.Body = "\r\nName:" + mail.Name + "\r\nMessage:" + mail.Message
+ "\r\nContact No.:" + mail.ContactNo + "\r\nEmail Address:" + mail.Email;
mailMessage.IsBodyHtml = true;

smtpClient.Send(mailMessage);
ViewBag.Message = "Your Message submitted successfully. " +
" We will contact you shortly. Thank You";
}
catch (Exception ex)
{
ViewBag.Message = ex.Message.ToString();
}

return View();
}
}
}

And edit its View as below:

Views>ContactUs>Index.cshtml:

@model Office365Mail.Models.Mail

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
</head>
<body style="padding:5px;">
@using (Html.BeginForm())
{
<div>
<h2>Send Mails using Office365 Mail address</h2>
<hr />
<div class="row">
<div class="col-lg-6">
<table class="table-condensed" cellpadding="5" cellspacing="5">
<tr>
<td>
Name:
</td>
<td>
@Html.TextBoxFor(m => m.Name)
</td>
</tr>
<tr>
<td>
Message:
</td>
<td>
@Html.TextAreaFor(m => m.Message)
</td>
</tr>
<tr>
<td>
Contact No.:
</td>
<td>
@Html.TextBoxFor(m => m.ContactNo)
</td>
</tr>
<tr>
<td>
Email Address:
</td>
<td>
@Html.TextBoxFor(m => m.Email)
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="submit" title="Submit"
class="btn btn-success"/>
</td>
</tr>
<tr>
<td align="center" colspan="2">
@ViewBag.Message
</td>
</tr>

</table>
</div>
</div>
</div>
}
</body>
</html>

mvc-smtp-send-mail-office365

Also see:

How to upload files in ASP.NET?

How to upload files in ASP.NET MVC C#?

ASP.NET How to download files?

How to upload and download files in ASP.NET with SQL Database?


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.