Send Mail To user using semp in MVC
Send Mail To user using semp in MVC
Code:
[Route("CountactUs")]
public ActionResult CountactUs()
{
return View();
}
[Route("CountactUs")]
[HttpPost]
public ActionResult CountactUs(Mymail md)
{
FormDbEntities db = new FormDbEntities();
tbl_mail tb = new tbl_mail()
{
FirstName=md.FirstName,
Gmail=md.Gmail,
Body=md.Body,
Subject=md.Subject
};
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("bhavybarasara@gmail.com", "123123123");
smtp.EnableSsl = true;
MailMessage msg = new MailMessage();
msg.Subject =md.Subject;
msg.Body = md.FirstName +" "+ md.Body;
string toaddress = md.Gmail;
msg.To.Add(toaddress);
msg.From = new MailAddress("Bhavybarasara@gmail.com");
try
{
smtp.Send(msg);
ViewData["MailSend"] = "mail sended throght smtp" ;
}
catch(Exception exp)
{
throw exp;
}
db.tbl_mail.Add(tb);
int a= db.SaveChanges();
if(a==1)
{
ViewData["MailRegistretion"] = "Registerd !!!";
ModelState.Clear();
}
else
{
ViewData["MailRegistretion"] = "Mail not sended !!!";
}
return View();
}
public ActionResult no()
{
return View();
}
}
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Test.Models
{
public class Mymail
{
public int id { get; set; }
[Required(ErrorMessage ="pleace enter First name")]
public string FirstName { get; set; }
[EmailAddress]
[Required(ErrorMessage ="pleace enter Your Gmail")]
public string Gmail { get; set; }
[Required(ErrorMessage ="Enter Your Subject")]
public string Subject { get; set; }
[Required(ErrorMessage ="Enter Your Gmail Body ")]
public string Body { get; set; }
}
}
Comments
Post a Comment