Using Web API Send Email To Gmail In ASP.NET MVC 5 c# 4.6

In this article will explain step by step process Using WEB API Sending email to gmail account in asp.net mvc c# code
To send an gmail in asp.net c# mvc web api require a Gmail SMTP host, port and network creditenals of your Gmail Id


SMTP = smtp.gmail.com
Port = 587
NetworkCredential = Username (your valid gmail id) and password (your gmail password)

After creating a project step 1, create a new class (Email Class. Cs) in the Models folder And add public properties
           
 
   public class EmailClass
    {        
        public string to { get; set; }
        public string body { get; set; }
        public string subject { get; set; }
    }

Step 2 : Next Create a new api controller in Controllers Folder and give name to api controller "Mulitpledisplay", after creating the new api controller
Now Add your model class in the namespace using Ex_Multiple_Model_WEBAPI_Single_View.Models;
Here " Ex_Multiple_Model_WEBAPI_Single_View " is my project name, and add the below code on your web api controller
	 using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Net.Mail;
using Ex_Multiple_Model_WEBAPI_Single_View.Models;


namespace Ex_Multiple_Model_WEBAPI_Single_View.Controllers
{
    public class MulitpledisplayController : ApiController
    {
         public IHttpActionResult email(EmailClass ec)
        {
            string subject = ec.subject;
            string body = ec.body;          
            string emailTo = ec.to;
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("username@gmail.com");
            mail.To.Add(emailTo);
            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = false;
            SmtpServer.UseDefaultCredentials = true;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("username@gmail.com", "gmailpassword");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
            return Ok();
        }
    }       
}
	

after adding the above code now run your application to get the web api URI on the browser type the http://localhost:58167/api/Mulitpledisplay
Ex: http://localhost or domainname/api/apicontrollername

replace the localhost with your localhost (domain name) and forward slash api forward slash your api controller name
Step - 3 Now In the same controllers folder, add a MVC 5 Controller empty And add the below code on your mvc controller
	
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ex_Multiple_Model_WEBAPI_Single_View.Models;
using System.Net.Http;

namespace Ex_Multiple_Model_WEBAPI_Single_View.Controllers
{
    public class MulitpleModelsController : Controller
    {
        // GET: MulitpleModels
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(EmailClass ec)
        {
            HttpClient hc = new HttpClient();
            hc.BaseAddress = new Uri("http://localhost:58167/api/Mulitpledisplay");

            var consumewebapi = hc.PostAsJsonAsync<EmailClass>("Mulitpledisplay", ec);
            consumewebapi.Wait();

            var sendmail = consumewebapi.Result;
            if(sendmail.IsSuccessStatusCode)
            {
                ViewBag.message = "The Mail Has Been Sent To " + ec.to.ToString();
            }          
            return View();
        }
    }
}
 

step 4 : Build the application before adding the view cshtml page to your project
step 5 : add a view page and place the code on your cshtml page
	  @model Ex_Multiple_Model_WEBAPI_Single_View.Models.EmailClass

@{
    ViewBag.Title = "Index";
}

Index

@using (Html.BeginForm("Index", "MulitpleModels", FormMethod.Post, new { enctype = "multipart/form-data" })) {
To: @Html.TextBoxFor(model => model.to)
Subject: @Html.TextBoxFor(model => model.subject)
Body: @Html.TextAreaFor(model => model.body, new { rows = "15", cols = "20" })

@ViewBag.Message }
Step - 5 Now the most important step make sure in the Routeconfig.cs file the controller and index name is correct, if not you application does not work get an error
step - 6 Final step press F5 to run the application, thats it ..

Free Top Udemy Courses

Read Complete Article → sadfasfasfd asdfasdfsa asfasfd asdfasdf asdfasfdsa asdfasdf