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("raj@mycompanyid.com");
    message.To.Add(new MailAddress("rajmvj@gmail.com"));

    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 = "smtp.sendgrid.net";
    client.Port = 587;
    client.EnableSsl = true;
    client.Send(message);

    Any help or suggestion would be apreciated.

    Regards,
  • #759437
    Hello Raj,

    Refer the below code to send email in MVC :

    MailMessage mail = new MailMessage();
    mail.To.Add("rajmvj@gmail.com");
    mail.From = new MailAddress(raj@mycompanyid.com);
    mail.Subject = "test subject";
    string Body = "test receiver";
    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 ("mysendgrid_username", "mysendgrid_password");// Enter sender's Username and Password
    smtp.EnableSsl = true;
    smtp.Send(mail);


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

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

  • #759443
    Hi,

    Refre the below code for sending mail using MVC applications.

    [HttpPost]
    [ValidateAntiForgeryToken]
    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("recipient@gmail.com")); // replace with valid value
    message.From = new MailAddress("sender@outlook.com"); // 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 = "user@outlook.com", // replace with valid value
    Password = "password" // replace with valid value
    };
    smtp.Credentials = credential;
    smtp.Host = "smtp-mail.outlook.com";
    smtp.Port = 587;
    smtp.EnableSsl = true;
    await smtp.SendMailAsync(message);
    return RedirectToAction("Sent");
    }
    }
    return View(model);
    }

    hope its useful for uu..

    Thanks,
    Chitaranjan

  • #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
    Hi
    You can go through the following link for send mail from mvc

    http://www.c-sharpcorner.com/UploadFile/rahul4_saxena/send-an-email-in-mvc-4-application/
    http://www.codeproject.com/Articles/712485/Send-Email-to-Selected-Recipients-from-your-ASP-NE
    http://www.codeproject.com/Tips/832559/Sending-an-E-Mail-With-Attachment-Using-ASP-Net-MV
    http://www.dotnetcodesg.com/Article/UploadFile/2/278/Send%20Mail%20In%20ASP.NET%20Using%20MVC%204.aspx

    Hope this helpful to you

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

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


  • Sign In to post your comments