为什么 SmtpClient 在 XP/Vista/Win7 上的行为不同

发布于 2024-08-19 12:03:51 字数 1283 浏览 3 评论 0原文

我正在使用以下代码,它似乎每次在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凡尘雨 2024-08-26 12:03:52

您是否在所有三个系统上使用相同版本的 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).

自我难过 2024-08-26 12:03:52

我怀疑这与操作系统有什么关系,这种类型的异常通常是从内部异常中冒出来的。捕获异常并查看内部异常,看看真正的问题是什么。

然而,此类问题通常是防火墙阻塞、远程 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.

初雪 2024-08-26 12:03:51

在 Windows 计算机上尝试以下操作:

  1. windows key + r
  2. 输入 cmd
  3. 输入 telnet smtp.gmail.com 587

如果显示连接被拒绝或类似信息那么就是防火墙或网络问题,与代码无关。

Try from the Windows machine the following:

  1. windows key + r
  2. Type cmd
  3. Type telnet smtp.gmail.com 587

If it says connection refused or similar then it's a firewall or network problem, unrelated with the code.

木有鱼丸 2024-08-26 12:03:51

很难说是否是这样,但我们曾经遇到过这个问题,而罪魁祸首是一款防病毒实用程序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文