【问题标题】:Finding the exact cause for the exception - System.Net.Sockets.SocketException查找异常的确切原因 - System.Net.Sockets.SocketException
【发布时间】:2014-01-17 16:31:40
【问题描述】:

我尝试使用下面给出的代码向 SMTP 邮件服务器发送邮件。但是,我收到一个错误 -

Error: System.Reflection.TargetInvocationException: Exception has 
been thrown by the target of an invocation. ---> System.Net.Mail.SmtpException:
Failure sending mail. ---> System.Net.WebException: Unable to connect to
the remote server ---> System.Net.Sockets.SocketException: 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 123.456.789:587
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, 
SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, 
Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState 
state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)

此异常告诉我可能的原因列表 - 连接 尝试失败,因为连接方在之后没有正确响应 一段时间,或建立连接失败,因为连接的主机 未能响应 123.456.789:587

但它并没有告诉我确切的原因是什么 - 端口号、错误的用户名、密码或其他东西。如何找出确切的原因。

这是我使用的代码 - Sending email with attachments from C#, attachments arrive as Part 1.2 in Thunderbird

using System.Net;
using System.Net.Mail;

public void email_send()
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("your mail@gmail.com");
    mail.To.Add("to_mail@gmail.com");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
    mail.Attachments.Add(attachment);

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("your 
    mail@gmail.com", "your password");
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);

}

【问题讨论】:

  • because the connected party did not properly respond after a period of time 该 IP 地址很可能没有服务器。
  • 它肯定告诉你不是用户名/密码,只是无法及时连接。检查 IP/端口。如果你知道怎么做,你可以通过 telnet 手动完成。
  • @DanielDiPaolo - 我如何确定是否可以使用 telnet 连接到邮件服务器?
  • @DanielDiPaolo 如果是端口问题,那么类似 connection denied 会返回。
  • 现在试试 - port25.com/…

标签: c# .net email


【解决方案1】:

那个IP地址显然不正确123.456.789:587
在命令提示符下尝试ping smtp.gmail.com 以查看它是否正在解析到该地址。如果是这样,你在某个地方已经覆盖了它,可能在你的 hosts 文件中(C:\Windows\System32\drivers\etc\hosts)。

在运行你的 sn-p 后,我立即从 Google 收到了“阻止登录尝试”,因为这种身份验证方法显然不符合他们当前的标准。

【讨论】:

  • 我不敢相信我没有意识到这个问题已经 2 岁了。有人把它复活到最近的问题中
猜你喜欢
  • 1970-01-01
  • 2013-05-08
  • 1970-01-01
  • 2022-10-13
  • 2021-11-05
  • 2015-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多