为什么 SmtpClient 在 XP/Vista/Win7 上的行为不同
我正在使用以下代码,它似乎每次在 Vista/Win7 上都能完美运行。
private void SendEmail(string subject, string body, string attach)
{
using (MailMessage message = new MailMessage("[email protected]", "[email protected]", subject, body))
{
message.IsBodyHtml = true;
if (!string.IsNullOrEmpty(attach))
{
Attachment attached = new Attachment(attach);
message.Attachments.Add(attached);
}
SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("[email protected]", "password"),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network
};
client.Send(message);
}
}
但是在 Windows XP 上我得到:
No connection could be made because the target machine actively refuses it
我已经检查过并且 Windows 防火墙已完全禁用...
I'm using the following code which appears to work perfectly every time on Vista/Win7.
private void SendEmail(string subject, string body, string attach)
{
using (MailMessage message = new MailMessage("[email protected]", "[email protected]", subject, body))
{
message.IsBodyHtml = true;
if (!string.IsNullOrEmpty(attach))
{
Attachment attached = new Attachment(attach);
message.Attachments.Add(attached);
}
SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("[email protected]", "password"),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network
};
client.Send(message);
}
}
However on Windows XP I'm getting:
No connection could be made because the target machine actively refuses it
I've checked and Windows firewall is completely disabled...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否在所有三个系统上使用相同版本的 System.Net.Mail?
此外,可能与 Windows 防火墙阻止连接(或某些其他防火墙)有关。
Are you using the same version of System.Net.Mail on all three systems ?
Also, could be related to the Windows Firewall blocking connections (or some other firewall).
我怀疑这与操作系统有什么关系,这种类型的异常通常是从内部异常中冒出来的。捕获异常并查看内部异常,看看真正的问题是什么。
然而,此类问题通常是防火墙阻塞、远程 smtp 服务器阻止传入请求或您的计算机阻止端口 25 上的传出请求。
I doubt this has anything to do with the OS, that type of exception is usually bubbled up from inner ones. Trap the exception and have a look into the inner exceptions and see what the real problem is.
However, this sort of problem is usually a firewall blockage, the remote smtp server is blocking incoming requests or your machine is blocking outgoing requests on port 25.
在 Windows 计算机上尝试以下操作:
cmd
telnet smtp.gmail.com 587
如果显示连接被拒绝或类似信息那么就是防火墙或网络问题,与代码无关。
Try from the Windows machine the following:
cmd
telnet smtp.gmail.com 587
If it says connection refused or similar then it's a firewall or network problem, unrelated with the code.
很难说是否是这样,但我们曾经遇到过这个问题,而罪魁祸首是一款防病毒实用程序。
Hard to say if this is it, but we had that problem at one point, and it was an antivirus utility that was the culprit.