查找异常的确切原因 - System.Net.Sockets.SocketException [英] Finding the exact cause for the exception - System.Net.Sockets.SocketException

查看:294
本文介绍了查找异常的确切原因 - System.Net.Sockets.SocketException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried to send a mail to an SMTP mail server using the code given below. But, I am getting an error -

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)

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

This exception tells me the possible list of causes - 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

但是它并没有告诉我究竟是什么原因 - 端口号,wron g用户名,密码或其他内容。如何找出确切的原因。

But it does not tell me exactly what is the cause - port number, wrong username, password or something else. How do I find out what is the exact cause.

这是我使用的代码 -
发送电子邮件附件从C#,附件作为第1.2部分到达Thunderbird

Here is the code I used - 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);

}


推荐答案

地址显然不正确 123.456.789:587

尝试 ping smtp.gmail.com 在命令提示符下查看是否解析到该地址。如果是这样,你可能在某个地方,也许在你的主机文件(C:\Windows\System32\drivers\etc\hosts)中。

That IP address is obviously not correct 123.456.789:587.
Try ping smtp.gmail.com in a command prompt to see if it is resolving to that address. If so, you have that overriden somewhere, perhaps in your hosts file (C:\Windows\System32\drivers\etc\hosts).

运行您的代码段后,我立即收到Google的登录尝试,因为这种身份验证方法显然不符合目前的标准。

After running your snippet ran but then I immediately got a "Sign-in attempt prevented" from Google since this method of authentication apparently does not meet their current standards.

这篇关于查找异常的确切原因 - System.Net.Sockets.SocketException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆