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
Post a Comment