【发布时间】:2017-07-01 14:11:42
【问题描述】:
我正在做一个网络项目。我想创建“忘记我的密码”系统并在需要时向用户发送邮件,但是当我尝试这样做时,它会给出错误,例如 SmtpException : Failure Sending Message, SocketException: Party did not proper response or connection failed because connected主机未能响应,无法发送邮件。我尝试了 2525、25 和 587 端口。我还检查了是否可以通过 mxtoolbox 连接 smtp.gmail.com:
这是 SMTP 电子邮件服务器测试
这是 SMTP DNS 测试
这是我的代码:
protected void lblForPass_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.UseDefaultCredentials = false;
client.Port = 25;
client.Timeout = 300000;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("mytestmail@gmail.com", "1122334455");
client.Credentials = credentials;
MailMessage msg = new MailMessage();
msg.From = new MailAddress("mytestmail@gmail.com");
msg.To.Add(new MailAddress("mytestmail2@gmail.com"));
msg.Subject = "Subject Test";
msg.IsBodyHtml = true;
msg.Body = string.Format("<html><head></head><body><b>Body Test</b></body>");
try
{
client.Send(msg);
ScriptManager.RegisterClientScriptBlock(this, GetType(), "Mail Status", "alert('Sended Successfuly')", true);
}
catch(Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
string err = ex.Message;
ScriptManager.RegisterClientScriptBlock(this, GetType(), "Mail Error", "alert('" +err+"')", true);
}
}
我在这个代码块中是否遗漏了什么或者我需要检查其他什么?谢谢。
【问题讨论】:
标签: c# asp.net smtp mail-server