System.Net.Mail.SmtpException: the service is unavailable, closing the transmission channel. Server response: 4.4.2

I get this error when I often send an email to the user list. Let's say it sends 10 letters, and 1 gives an error, then sends a couple more letters and gives the same error.

The code is as follows:

public static bool SendEmail(string toMail, string fromname, string from, string subject, string body, string BCC) { MailMessage mailmessage = new MailMessage(" frommail@mail.com ", 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; } 

And the error I get is this:

System.Net.Mail.SmtpException: the service is unavailable, closing the transmission channel. Server response: 4.4.2 mailer.mailer.com Error: timeout exceeded in System.Net.Mail.MailCommand.CheckResponse (SmtpStatusCode statusCode, string response) in System.Net.Mail.MailCommand.Send (smtpConnection command, Byte command [], line from) on System.Net.Mail.SmtpTransport.SendMail (sender MailAddress, recipients MailAddressCollection, String deliveryNotify, SmtpFailedRecipientException exception and exception) in System.Net.Mail.SmtpClient.Send (MailMessage message)

Can anyone help, this mistake is killing me.

Thanks in advance.

+6
source share
2 answers

Recycling smtpclient (objCompose) did the trick.

  // 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(); 
+8
source

I like to wrap it in the used block. It will force dispose, and it is very elegant.

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

Source: https://habr.com/ru/post/908440/


All Articles