SmtpClient:连接尝试失败,因为连接方在一段时间后没有正确响应 [英] SmtpClient: A connection attempt failed because the connected party did not properly respond after a period of time
问题描述
在 Visual Studio 2008 中使用 C#.NET 发送电子邮件时,出现以下错误
While working with Email sending in C#.NET in visual studio 2008 i got the below error
连接尝试失败,因为连接方在一段时间后没有正确响应,或建立连接失败,因为连接的主机没有响应 74.125.53.108:25
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 74.125.53.108:25
但是相同的代码在其他 PC 上运行良好,但是当我今天测试时,它在 Send()
方法中出现错误......而且我的网络连接很好,我正在测试电子邮件代码..
But the same code was working fine in some other PC but when i am testing today it gives me error in Send()
method... Also my network connection is good where i am testing the email code..
下面是我的邮箱代码
MailMessage mail = new MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential("MyUserName@gmail.com",
"MyPassword");
smtp.EnableSsl = true;
smtp.Send(mail);
出现这种错误的原因可能是什么..???
What could be the reasons for such error..???
推荐答案
以下代码对我有用.你的代码给了我错误,我相信这是由于没有将端口设置为 587.
The following code works for me. Your code was giving me errors, I believe it was due to not setting the port to 587.
http://forums.asp.net/t/1250771.aspx/4/10
MailMessage mail = new MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com",587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(address, password);
smtp.Send(mail);
这篇关于SmtpClient:连接尝试失败,因为连接方在一段时间后没有正确响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!