2012-02-14 43 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