C# 从控制台应用程序发送电子邮件时发生套接字异常的原因是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12692064/
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
What caused the Socket Exception while sending email from Console application?
提问by anetafr
I'm trying to write a basic console app that will send an email. The problem is that I keep getting the Socket exception:
我正在尝试编写一个可以发送电子邮件的基本控制台应用程序。问题是我不断收到 Socket 异常:
An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.x.xxx:25
尝试以访问权限 xxx.xxx.x.xxx:25 禁止的方式访问套接字
I turned off my windows firewall but it didn't change anything. I tried to run it with and without credentials specified. I also tried to use different smtp servers including smtp.mail.yahoo.com with port 25 and my credentials but with no luck. What is the problem causing this exception?
我关闭了我的 Windows 防火墙,但它没有改变任何东西。我尝试在指定和不指定凭据的情况下运行它。我还尝试使用不同的 smtp 服务器,包括 smtp.mail.yahoo.com 与端口 25 和我的凭据,但没有运气。导致此异常的问题是什么?
I'm running VS2008 on Windows 7. Below is my code sample and the exception I get.
我在 Windows 7 上运行 VS2008。下面是我的代码示例和我得到的异常。
static void Main(string[] args)
{
String recipient = "[email protected]";
String sender = "[email protected]";
MailMessage message = new MailMessage();
message.From = new MailAddress(sender);
message.To.Add(new MailAddress(recipient));
message.Subject = "Subject";
message.Body = "Body";
SmtpClient smtp = new SmtpClient
{
Host = "smtp.server",
Port = 25,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
//UseDefaultCredentials = false,
//Credentials = new NetworkCredential("validemail", "validpassword"),
Timeout = 3000
};
try
{
smtp.Send(message);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
And the exception I got:
我得到的例外是:
System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.x.xxx:25
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)
--- End of inner exception stack trace ---
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket
6, Int32 timeout)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at ...Program.Main(String[] args) in ...\Program.cs:line xxx
采纳答案by Mariusz.W
I had very similar situation once and it turned out that the antivirus was just blocking that port. Your possible options are listed here: http://social.msdn.microsoft.com/Forums/br/transactsql/thread/c083c2c6-f74e-42cd-a811-4329ff7aa5f1
我曾经遇到过非常相似的情况,结果发现防病毒软件只是阻止了该端口。此处列出了您可能的选项:http: //social.msdn.microsoft.com/Forums/br/transactsql/thread/c083c2c6-f74e-42cd-a811-4329ff7aa5f1
回答by Damian Schenkelman
Is port 25 enabled and available? For example this threadexplains that is is possible for another service to have exclusive access to it.
端口 25 是否已启用且可用?例如,这个线程解释了另一个服务可以独占访问它。
Have you tried this code in another machine (with less security)? I don't mean that as part of a fix, but just to make sure that the code is OK by itself.
您是否在另一台机器上尝试过此代码(安全性较低)?我并不是说这是修复的一部分,而是为了确保代码本身没有问题。
回答by Andrew
Try to play with EnableSsl. In msdn reason of exception:
尝试使用 EnableSsl。在 msdn 中的异常原因:
EnableSsl is set to true, but the SMTP mail server did not advertise STARTTLS in the response to the EHLO command.
EnableSsl 设置为 true,但 SMTP 邮件服务器未在对 EHLO 命令的响应中通告 STARTTLS。
Also check StatusCode of the exception.
还要检查异常的 StatusCode。
回答by Murtuza Kabul
Either don't assign the port or don't use EnableSSL = true. Doing one of this will solve your problem.
要么不分配端口,要么不使用 EnableSSL = true。执行其中之一将解决您的问题。
You are trying to initiate a communication which is not allowed to be done this way.
您正在尝试发起不允许以这种方式进行的通信。

