Jonathan
Jonathan

Reputation: 1027

Cannot send email in ASP.NET MVC

I would like to send email in ASP.NET MVC and here my code:

public ActionResult Index(MyMailModel objMailModel)
    {
        if (ModelState.IsValid)
        {
            string from = "******@gmail.com";
            using (MailMessage mail = new MailMessage(from, objMailModel.To))
            {
                mail.Subject = "Backup Database";
                mail.Body = "hahaha";
                mail.IsBodyHtml = false;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential network = new NetworkCredential(from, "*******");                    
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = network;
                smtp.Port = 587;
                smtp.Send(mail);
                ViewBag.Message = "Sent";
                return View("Index", objMailModel);
            }
        }
        else
        {
            return View();
        }

    }

But, I have a error:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ******:587**

Please help me.

Thanks.

Upvotes: 2

Views: 1031

Answers (1)

Sergey Shabanov
Sergey Shabanov

Reputation: 176

Check if firewall or anti-virus or port is blocking the response from the server

Upvotes: 1

Related Questions