2012-02-14 45 views
6

当我经常向用户列表发送一些电子邮件时,出现此错误。假设它发送10封邮件,1发出错误,然后发送更多邮件并给出相同的错误。System.Net.Mail.SmtpException:服务不可用,关闭传输通道。服务器响应是:4.4.2

的代码看起来是这样的:

public static bool SendEmail(string toMail, string fromname, string from, string subject, string body, string BCC) 
    { 

     MailMessage mailmessage = new MailMessage("[email protected]", toMail, subject, body); 
     mailmessage.IsBodyHtml = true; 
     mailmessage.BodyEncoding = Encoding.GetEncoding(1254); 
     mailmessage.SubjectEncoding = Encoding.GetEncoding(1254); 

     SmtpClient objCompose = new SmtpClient("xxxx"); 

     try 
     { 
      objCompose.Send(mailmessage); 

      return true; 
     } 
     catch (Exception ex) { 

     } 

     return false; 
    } 

而我得到的错误是这样的:

System.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: 4.4.2 mailer.mailer.com Error: timeout exceeded at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message)

任何人都可以请大家帮忙,这个错误是我的命。

在此先感谢。

回答

8

处理smtpclient(objCompose)的伎俩。

// Summary: 
    //  Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, 
    //  and releases all resources used by the current instance of the System.Net.Mail.SmtpClient 
    //  class. 
    public void Dispose(); 
+0

MailMessage.Dispose()(.NET 3.5) – garik 2012-06-06 13:36:43

+3

没有为我工作。那么......它摆脱了这个例外,但现在它只是在发送一两封电子邮件时挂起。 – 2012-11-30 04:41:21

4

我喜欢把它包装在一个使用块中。这将迫使处置,它非常优雅。

using(SmtpClient objCompose = new SmtpClient("xxxx")) 
{ 
    objCompose.Send(mailmessage); 
} 
+1

错误:无法将类型SmtpClient隐式转换为IDisposal – 2013-07-07 13:57:54

+0

此代码在VS 2013,.NET 4.5下编译得很好 – nsimeonov 2014-11-03 17:08:44