2012-02-14 16 views
6

Tôi gặp lỗi này khi tôi thường xuyên gửi một số e-mail đến danh sách người dùng. Nói nó gửi 10 thư và 1 cho một lỗi, sau đó gửi một vài thư và cung cấp cho cùng một lỗi.System.Net.Mail.SmtpException: Dịch vụ không khả dụng, đóng kênh truyền dẫn. Phản hồi của máy chủ là: 4.4.2

Mã này trông như thế này:

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; 
    } 

Và lỗi tôi nhận được là:

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)

có thể bất cứ ai xin vui lòng giúp đỡ, lỗi này là giết chết tôi.

Xin cảm ơn trước.

Trả lời

8

Xử lý smtpclient (objCompose) đã thực hiện thủ thuật.

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

+3

Không hoạt động cho tôi. Vâng ... nó đã loại bỏ ngoại lệ nhưng bây giờ nó chỉ treo khi một người gửi một hoặc hai email. Lỗi –

4

Tôi thích gói nó trong một khối sử dụng. Điều đó sẽ buộc phải vứt bỏ và nó rất tao nhã.

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

: không thể chuyển đổi hoàn toàn loại SmtpClient thành IDisposal –

+0

Mã này biên dịch tốt dưới VS 2013, .NET 4.5 – nsimeonov

Các vấn đề liên quan