New connection to send email in C# / .NET -


i facing problem in sending email. webhost/emailhost instructed me can not send more 10 emails in 1 connection. sending 30-40 emails using following code:

smtpclient emailserver = new smtpclient("server"); emailserver.credentials = new system.net.networkcredential("username", "password");  (int icount = 0; icount < listemail.count; icount++) {    mailmessage email = new mailmessage();    email.from = new mailaddress("from");    email.subject = "subject";    email.to.add(listemail[icount]);     emailserver.send(email); } 

but if put code

smtpclient emailserver = new smtpclient("server"); emailserver.credentials = new system.net.networkcredential("username", "password"); 

in for loop like:

for (int icount = 0; icount < listemail.count; icount++) {        smtpclient emailserver = new smtpclient("server");    emailserver.credentials = new system.net.networkcredential("username", "password");     mailmessage email = new mailmessage();    email.from = new mailaddress("from");    email.subject = "subject";    email.to.add(listemail[icount]);     emailserver.send(email); } 

then create new connection server every time email sent? or should wait few minutes make sure previous connection expires before creating new one? mean not know how create new connection each email send , how ensure send each email new connection email server.

change code following:

using (var emailserver = new smtpclient("server")) {    emailserver.credentials = new system.net.networkcredential("username", "password");     mailmessage email = new mailmessage();    email.from = new mailaddress("from");    email.subject = "subject";    email.to.add(listemail[icount]);     emailserver.send(email); } 

than can sure connection closed. try use using statement on disposable objects, make sure cleared after done.


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -