Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么SmtpClient在XP/Vista/Win7上的行为会有所不同_C#_.net_Gmail_Smtpclient - Fatal编程技术网

C# 为什么SmtpClient在XP/Vista/Win7上的行为会有所不同

C# 为什么SmtpClient在XP/Vista/Win7上的行为会有所不同,c#,.net,gmail,smtpclient,C#,.net,Gmail,Smtpclient,我正在使用下面的代码,它似乎每次在Vista/Win7上都能完美地工作 private void SendEmail(string subject, string body, string attach) { using (MailMessage message = new MailMessage("username@gmail.com", "username@gmail.com", subject, body)) { message.IsBodyHtml = t

我正在使用下面的代码,它似乎每次在Vista/Win7上都能完美地工作

private void SendEmail(string subject, string body, string attach)
{
    using (MailMessage message = new MailMessage("username@gmail.com", "username@gmail.com", 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("username@gmail.com", "password"),
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network
        };

        client.Send(message);
    }
}
然而,在Windows XP上,我得到了:

No connection could be made because the target machine actively refuses it

我已检查,Windows防火墙已完全禁用…

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

  • windows键+r
  • 键入
    cmd
  • 键入
    telnet smtp.gmail.com 587

  • 如果它说连接被拒绝或类似,那么这是防火墙或网络问题,与代码无关。

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

    您在所有三个系统上都使用相同版本的System.Net.Mail吗


    另外,可能与Windows防火墙阻止连接(或其他防火墙)有关。

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


    但是,这类问题通常是防火墙阻塞、远程smtp服务器阻塞传入请求或您的计算机阻塞端口25上的传出请求。

    telnet失败。出于某种原因,我们所有的XP机器都会出现这种情况,而Vista Win7机器都不会。我必须看看他们都安装了什么…但是请注意,
    telnet.exe
    在某些版本的Windows上默认不安装。