从控制台应用程序发送电子邮件时导致套接字异常导致什么? [英] What caused the Socket Exception while sending email from Console application?

查看:168
本文介绍了从控制台应用程序发送电子邮件时导致套接字异常导致什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基本的控制台应用程序,它将发送一封电子邮件。问题是我不断得到Socket异常:

I'm trying to write a basic console app that will send an email. The problem is that I keep getting the Socket exception:


尝试以禁止其访问权限xxx的方式访问套接字.xxx.x.xxx:25

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和我的凭据,但没有运气。导致此异常的问题是什么?

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 7上运行VS2008。下面是我的代码示例和异常。

I'm running VS2008 on Windows 7. Below is my code sample and the exception I get.

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, 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


推荐答案

我有一个非常相似的情况,事实证明,防病毒只是阻止该端口。您可能的选项列在此处:
http:/ /social.msdn.microsoft.com/Forums/br/transactsql/thread/c083c2c6-f74e-42cd-a811-4329ff7aa5f1

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

这篇关于从控制台应用程序发送电子邮件时导致套接字异常导致什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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