MailMessage mail = new MailMessage();
mail.To.Add("my_id@gmail.com");
mail.From = new MailAddress("some_id@gmail.com");
mail.Subject = "test";
string Body = "test";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 495; //Also tried with 587
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("some_id@gmail.com", "passwordOfSome_id");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Send(mail);
我正在尝试在我的localhost上运行此代码。此代码是在IIS7上托管的MVC3 Web应用程序中编写的。我收到此SMTP例外 -
Failure sending mail (Message=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 173.194.66.109:587.
我也尝试过交换地址和地址。请帮忙!
答案 0 :(得分:1)
您可能想尝试在您的Gmail帐户中启用对“安全性较低的应用”的支持,因为您的代码看起来很好。虽然我坚持使用587端口。