c# - SmtpClient: The maximum number of concurrent connections has exceeded a limit -


i have following method call every mailmessage:

public static string sendemail(mailmessage email)         {             string rez = "";              try             {                 var smtpserver = "10.xxx.xx.xx";                   using (smtpclient mailclient = new smtpclient())                 {                    mailclient.host = smtpserver;                     mailclient.send(email);                  }                  rez = "ok";             }             catch (exception ex)             {                 rez = "not ok: " + ex.message;             }              return rez;         } 

i send 32 email-s @ once, , 2 of them got following error mailclient.send(): not ok: service not available, closing transmission channel. server response was: 4.3.2 maximum number of concurrent connections has exceeded limit, closing transmission channel

i wondering if because created new smtpclient instance every mail?

will following change fix problem since there 1 instance of smtpclient. unfortunatellyi cannot test it, can try in production.

public static smtpclient mailclient = new smtpclient("10.xxx.xx.xx");   public static string sendemail(mailmessage email)             {                 string rez = "";                  try                 {                         mailclient.send(email);                       rez = "ok";                 }                 catch (exception ex)                 {                     rez = "not ok: " + ex.message;                 }                  return rez;             } 

thanks.

you can reuse instance of smtpclient send emails , practice (see msdn), think not solve problem.

from msdn

the connection established current instance of smtpclient class smtp server may re-used if application wishes send multiple messages same smtp server. particularly useful when authentication or encryption used establish connection smtp server. process of authenticating , establishing tls session can expensive operations. requirement re-establish connection each message when sending large quantity of email same smtp server have significant impact on performance. there number of high-volume email applications send email status updates, newsletter distributions, or email alerts. many email client applications support off-line mode users can compose many email messages sent later when connection smtp server established. typical email client send smtp messages specific smtp server (provided internet service provider) forwards email other smtp servers.

the smtpclient class implementation pools smtp connections can avoid overhead of re-establishing connection every message same server. application may re-use same smtpclient object send many different emails same smtp server , many different smtp servers. result, there no way determine when application finished using smtpclient object , should cleaned up.

when smtp session finished , client wishes terminate connection, must send quit message server indicate has no more messages send. allows server free resources associated connection client , process messages sent client.

the smtpclient class has no finalize method, application must call dispose explicitly free resources. dispose method iterates through established connections smtp server specified in host property , sends quit message followed gracefully ending tcp connection. dispose method releases unmanaged resources used socket , optionally disposes of managed resources. call dispose when finished using smtpclient. dispose method leaves smtpclient in unusable state. after calling dispose, must release references smtpclient garbage collector can reclaim memory smtpclient occupying.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -