i want to send an email using a asp.net mvc 4 application i am using this method
public void send_mail(ProcRec.Models.AccuseReception _objModelMail)
{
MailMessage mail = new MailMessage();
mail.To.Add(_objModelMail.To);
mail.From = new MailAddress(_objModelMail.From);
mail.Subject = _objModelMail.Subject;
string Body = _objModelMail.Body;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("username", "password");// Enter seders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
}
My model :
public class AccuseReception
{
public string From { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
controller :
if (ModelState.IsValid)
{
AccuseReception confermation_mail = new AccuseReception {
From="[email protected]",
To ="[email protected]",
Subject ="mail_de confermation",
Body = "votre demande est enregistrer ",
};
send_mail(my_mail);
}
i get no error but the mail is not sending and the ModelState is not valid
Postal. In Postal you build views that are sent as emails: aboutcode.net/postal