使用 asp.net 发送邮件失败.连接主机无法响应

本文关键字:主机 连接 响应 失败 net asp 使用 | 更新日期: 2023-09-27 18:31:15

我想从我的 c# 应用程序发送一个简单的测试邮件

我正在使用以下代码

MailMessage mail = new MailMessage("from", "to");
mail.Subject = "TestEmailImportant";
mail.Body = "This mail is to test if this program is working";
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.Credentials = new System.Net.NetworkCredential()
{
    UserName = "xxxxxxxxx@gmail.com",
    Password = "xxxxxx"
};
smtpClient.EnableSsl = true;
smtpClient.Send(mail);
Label1.Text = "Email Sent";

这是在单击按钮时触发的。

每当我单击该按钮时,浏览器都会一直"等待主机",大约一分钟后我收到此错误

连接尝试失败,因为连接的一方没有 一段时间后正确响应或建立连接 失败,因为连接的主机无法响应"某些 IP"。

指向smtpClient.Send(mail);行。

使用 asp.net 发送邮件失败.连接主机无法响应

注册 Gmail,然后转到 https://www.google.com/settings/security/lesssecureapps,您可以在其中查看设置。访问安全性较低的应用

Turn off (default) ==> Turn On    

添加此代码后,

  MailMessage mail = new MailMessage("fromm@gmail.com", "toaddress@gmail.com");
                mail.Subject = "TestEmailImportant";
                mail.Body = "This mail is to test if this program is working";
                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                smtpClient.Credentials = new System.Net.NetworkCredential()
                {
                    UserName = "XXXXXX@gmail.com",
                    Password = "YYYYYYYY"
                };
                smtpClient.EnableSsl = true;
                System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s,
                        System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                        System.Security.Cryptography.X509Certificates.X509Chain chain,
                        System.Net.Security.SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                };
                smtpClient.Send(mail);