我正在编写一个基本的控制台应用程序,它将发送一封电子邮件.问题是我不断得到Socket异常:
An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.x.xxx:25
我关掉了我的Windows防火墙,但没有改变任何东西.我试图运行它没有指定的凭据.我也尝试使用不同的smtp服务器,包括smtp.mail.yahoo.com与端口25和我的凭据,但没有运气.导致此异常的问题是什么?
我在Windows 7上运行VS2008.下面是我的代码示例和我得到的异常.
static void Main(string[] args) { String recipient = "recipient@email.com"; String sender = "sender@email.com"; 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()); } }
而我得到的例外:
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,Socket& abortSocket,Socket& abortSocket 6,Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject,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
解决方法
我有一个非常相似的情况,事实证明,防病毒只是阻止该端口.您可能的选项列在这里:
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
原文地址:https://www.jb51.cc/csharp/97559.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。