【发布时间】: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/…