You must Sign In to post a response.
  • Category: ASP.Net MVC

    Sending email in MVC

    Hi All,

    I have the below code to send an email in MVC. But getting "Failure Sending email" error.

    MailMessage message = new MailMessage();
    message.From = new MailAddress("");
    message.To.Add(new MailAddress(""));

    message.IsBodyHtml = true;
    message.BodyEncoding = Encoding.UTF8;
    message.Subject = "test subject";
    message.Body = "test receiver";

    SmtpClient client = new SmtpClient();
    var credential = new NetworkCredential
    UserName = "mysendgrid_username", // replace with valid value
    Password = "mysendgrid_password" // replace with valid value
    client.Credentials = credential;
    client.Host = "";
    client.Port = 587;
    client.EnableSsl = true;

    Any help or suggestion would be apreciated.

  • #759437
    Hello Raj,

    Refer the below code to send email in MVC :

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(;
    mail.Subject = "test subject";
    string Body = "test receiver";
    mail.Body = Body;
    mail.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "";
    smtp.Port = 587;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new System.Net.NetworkCredential ("mysendgrid_username", "mysendgrid_password");// Enter sender's Username and Password
    smtp.EnableSsl = true;

    Hope this will help you.
    Mark the answer if it helped you.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #759443

    Refre the below code for sending mail using MVC applications.

    public async Task<ActionResult> Contact(EmailFormModel model)
    if (ModelState.IsValid)
    var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
    var message = new MailMessage();
    message.To.Add(new MailAddress("")); // replace with valid value
    message.From = new MailAddress(""); // replace with valid value
    message.Subject = "Your email subject";
    message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
    message.IsBodyHtml = true;

    using (var smtp = new SmtpClient())
    var credential = new NetworkCredential
    UserName = "", // replace with valid value
    Password = "password" // replace with valid value
    smtp.Credentials = credential;
    smtp.Host = "";
    smtp.Port = 587;
    smtp.EnableSsl = true;
    await smtp.SendMailAsync(message);
    return RedirectToAction("Sent");
    return View(model);

    hope its useful for uu..


  • #759447
    Please make sure that the credentials you are using to send email are correct and the email server and other details are valid.
    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #760051
    You can go through the following link for send mail from mvc

    Hope this helpful to you

    Name : Dotnet Developer-2015
    Email Id :

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • Sign In to post your comments