2

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.

Jonathan
  • 1,017
  • 10
  • 21
  • See http://stackoverflow.com/questions/20882891/how-can-i-send-email-using-gmail-smtp-in-asp-net-mvc-application you might need to try a different port – Jason Sep 04 '14 at 03:58
  • See [Sending email in .NET through Gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) or [Solved ..A connection attempt failed because the connected party did not properly respond](http://stackoverflow.com/questions/24671471/solved-a-connection-attempt-failed-because-the-connected-party-did-not-properl) – CodeCaster Sep 04 '14 at 10:38

1 Answers1

1

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

Sergey Shabanov
  • 176
  • 2
  • 11
  • My reputation score was < 50 to put any comments. Thanks for downvote. – Sergey Shabanov Sep 04 '14 at 10:41
  • You're welcome. Comments are for guesses, if you can't comment you shouldn't guess in an answer instead. You can try to rewrite your answer to state facts, like "This is caused by X, change Y to fix". – CodeCaster Sep 04 '14 at 10:50
  • One more smarty. Once again: there was no way to post comment, but there was a wish to help author. Because my "guess" is the main reason for this type on error. – Sergey Shabanov Sep 05 '14 at 01:39