2012-02-14 13 views
6

Viene visualizzato questo errore quando invio frequentemente alcuni messaggi di posta elettronica a un elenco di utenti. Diciamo che manda 10 mail e 1 dà un errore, quindi invia un altro paio di mail e dà lo stesso errore.System.Net.Mail.SmtpException: servizio non disponibile, chiusura del canale di trasmissione. La risposta del server è stata: 4.4.2

Il codice simile a questo:

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

E l'errore che ottengo è questo:

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)

Qualcuno può aiutare, questo bug mi sta uccidendo.

Grazie in anticipo.

risposta

8

Smaltare lo smtpclient (objCompose) ha funzionato.

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

non ha funzionato per me. Bene ... si è sbarazzato dell'eccezione, ma ora si blocca solo quando si invia un'email o due. Errore –

4

Mi piace avvolgerlo in un blocco di utilizzo. Ciò costringerà il dispose ed è molto elegante.

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

: impossibile convertire implicitamente il tipo SmtpClient in IDisposal –

+0

Questo codice viene compilato correttamente in VS 2013, .NET 4.5 – nsimeonov

Problemi correlati