C# SMTPException:无法从传输连接读取数据:net_io_connectionclosed
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17497154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SMTPException : Unable to read data from the transport connection: net_io_connectionclosed
提问by Hilmi
I know that this question looks like duplicate of dozens other questions while its not.
我知道这个问题看起来像其他几十个问题的重复,而事实并非如此。
When ever i try to send an email on my local machine through my web application an SMTPException is thrown and the exceptions is :
每当我尝试通过我的 Web 应用程序在本地计算机上发送电子邮件时,都会抛出 SMTPException 异常,并且异常是:
//on this line : SmtpServer.Send(mail);
Unable to read data from the transport connection: net_io_connectionclosed.
While the code on production is working perfectly, the same code, the same connections, the same credentials, i'm using IP instead of alias, i tried to turn off the firewall on my local machine, and nothing helped me to fix this issue.
虽然生产中的代码运行良好,但相同的代码、相同的连接、相同的凭据,我使用的是 IP 而不是别名,我尝试关闭本地计算机上的防火墙,但没有任何帮助我解决此问题.
While on my local machine is used to work previously, can anyone give just a hint what could be the problem that raising this issue?
虽然以前在我的本地机器上可以正常工作,但有人可以提示一下引发此问题的可能是什么问题吗?
采纳答案by Robert McKee
If you are on a residential internet connection, quite often your ISP will block outgoing email sends by blocking all outbound connections to port 25. This is quite common here in the US. Try connecting to a local email server over TCP/IP, or to one on your own internal network.
如果您使用的是住宅互联网连接,您的 ISP 通常会通过阻止所有到端口 25 的出站连接来阻止外发电子邮件发送。这在美国很常见。尝试通过 TCP/IP 连接到本地电子邮件服务器,或连接到您自己的内部网络上的一个。
回答by shamp00
This threadcontains some.
这个线程包含一些。
For instance: it looks like assigning a static IPmight solve the problem.
例如:看起来分配一个静态 IP可能会解决问题。
What this error means is that System.net.mail was unable to find the smtp server.
The answer will vary depending on whether you have a fixed IP or a dynamic IP but, basically, you need to assign a valid IP to your smtp server.
With fixed IP's this is relatively straightforward. With dynamic IP's it takes a bit of tweaking.
Open the IIS Manager and check the properties for the smtp server.
In the Default SMTP Virtual Server's properties, in the "Access" tab, in the Connection Control and Relay dialogs, make sure that your local IP is assigned. ( In my case, it's 10.0.0.2... )
You may also need to modify your hosts file, to point 127.0.0.1 to your machine name. ( \WINDOWS\system32\drivers\etc\hosts )
Then, in your code, assign your machine name to the smtp client :
Dim client As New SmtpClient("yourmachinename") client.Send(mail)
此错误意味着 System.net.mail 无法找到 smtp 服务器。
答案取决于您拥有固定 IP 还是动态 IP,但基本上,您需要为 smtp 服务器分配一个有效的 IP。
对于固定 IP,这相对简单。使用动态 IP 需要一些调整。
打开 IIS 管理器并检查 smtp 服务器的属性。
在默认 SMTP 虚拟服务器的属性中,在“访问”选项卡中,在连接控制和中继对话框中,确保已分配本地 IP。(就我而言,它是 10.0.0.2...)
您可能还需要修改您的主机文件,将 127.0.0.1 指向您的机器名称。( \WINDOWS\system32\drivers\etc\hosts )
然后,在您的代码中,将您的机器名称分配给 smtp 客户端:
Dim client As New SmtpClient("yourmachinename") client.Send(mail)
Alternatively another guy in the same thread seems to have found a workaroundfor the SMTP connection not being correctly closed.
或者,同一线程中的另一个人似乎找到了SMTP 连接未正确关闭的解决方法。
Set
SmtpClient.ServicePoint.MaxIdleTime = 1according to a supported work-around: http://connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID=146711which makes all smtp work properly.Here's a complete sample:
MailMessage msgMail = new MailMessage(); msgMail.To.Add(new MailAddress("[email protected]")); msgMail.Subject = "Message from web"; msgMail.IsBodyHtml = true; msgMail.Body = "Test message"; SmtpClient Client = new SmtpClient(); /* uses settings form web.config */ Client.ServicePoint.MaxIdleTime = 1; /* without this the connection is idle too long and not terminated, times out at the server and gives sequencing errors */ Client.Send(msgMail); msgMail.Dispose();
SmtpClient.ServicePoint.MaxIdleTime = 1根据支持的变通方法设置:http: //connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID = 146711,这使得所有 smtp 都能正常工作。这是一个完整的示例:
MailMessage msgMail = new MailMessage(); msgMail.To.Add(new MailAddress("[email protected]")); msgMail.Subject = "Message from web"; msgMail.IsBodyHtml = true; msgMail.Body = "Test message"; SmtpClient Client = new SmtpClient(); /* uses settings form web.config */ Client.ServicePoint.MaxIdleTime = 1; /* without this the connection is idle too long and not terminated, times out at the server and gives sequencing errors */ Client.Send(msgMail); msgMail.Dispose();
回答by Hardik Chandi
The Gmail account you are using has limit for number of emails being sent out per day/month and if you exceed that limit, it will reject any further outgoing emails.
您使用的 Gmail 帐户对每天/每月发送的电子邮件数量有限制,如果您超过该限制,它将拒绝任何进一步的外发电子邮件。
回答by dongfangshi
System.Net.NetworkCredential(user, password); ///Wrong
System.Net.NetworkCredential(email, password); ///right
System.Net.NetworkCredential(用户,密码);///错误的
System.Net.NetworkCredential(email, password); ///对
because some email server is shability
因为一些电子邮件服务器是 shability

