发送邮件时出现C#错误:已连接的套接字上发出连接请求

时间:2012-07-02 14:50:47

标签: c# email socketexception

我见过很多从c#代码发送电子邮件的例子,我遵循最流行的使用System.Net.Mail.SmtpClient的方法。我能够第一次发送邮件,但之后它会出错在已连接的套接字上发出连接请求

我使用以下代码发送邮件:

using (MailMessage mail = new MailMessage())
{
    using (SmtpClient mailer = new SmtpClient("smtp.server.com"))
    {
        Console.Write("Sending mails...");
        mail.From = new MailAddress("from@mail.com");
        mail.To.Add("myaddress@mail.com");
        mail.Subject = "test-subject";
        mail.Body = "test-body";
        mailer.Port = 25;
        mailer.Credentials = new System.Net.NetworkCredential("from@mail.com", "from-password");
        mailer.EnableSsl = true;
        try
        {
            mailer.Send(mail);
            Console.WriteLine("Mail sent");
        }
        catch (Exception e)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("\rCannot send mail, an error occured : {0}", e);
            Console.ResetColor();
        }
    }
}

错误信息是:

Cannot send mail, an error occured : System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connect request was made on an already connected socket 200.200.200.200: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& abortSocket6, 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(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at TestApp.Program.Main(String[] args) in D:\Desktop\TestApp\TestApp\Program.cs:line 290

如您所见,我在MailMessage子句中保留了SmptClientusing的实例,这样如果发生任何错误,连接将自动关闭。但是它在正确启动后首次发送邮件,之后所有其他尝试都失败了。

SMTP服务器位于代理后面,防火墙客户端正在我的系统上运行。就我认为而言,代理不是问题。

代码中是否有错误?我怎样才能让它发挥作用?

我尝试过使用Outlook API,但在发送每封邮件之前需要用户进行身份验证。我不想要这种行为。

PS:我删除了一些像IP地址和服务器名称这样的细节,在这种情况下无关紧要。

提前致谢:)

1 个答案:

答案 0 :(得分:1)

可能你应该升级到.NET 4.0。 Microsoft Connect上的This question说明SMTPClient没有正确关闭连接,并且此问题已在4.0中修复

有人使用属性 MaxIdleTime 设置为1取得了一些成功,但无论如何,当连接数超过邮件服务器允许的数量时,问题似乎只会向前移动