代码之家  ›  专栏  ›  技术社区  ›  Tim

为什么smtpclient在xp/vista/win7上的行为会不同?

  •  1
  • Tim  · 技术社区  · 15 年前

    我正在使用以下代码,在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防火墙已完全禁用…

    4 回复  |  直到 15 年前
        1
  •  2
  •   Andreas Bonini    15 年前

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

    1. 视窗键+R
    2. 类型 cmd
    3. 类型 telnet smtp.gmail.com 587

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

        2
  •  1
  •   Mark Wilkins    15 年前

    很难说这是不是真的,但我们曾经遇到过这个问题,而罪魁祸首是杀毒工具。

        3
  •  0
  •   Nick Haslam    15 年前

    您是否在所有三个系统上都使用相同版本的system.net.mail?

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

        4
  •  0
  •   James    15 年前

    我怀疑这和操作系统有什么关系,这种类型的异常通常是由内部异常引起的。捕获异常并查看内部异常,看看真正的问题是什么。

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