asp.net - Try Catch and then continue -
i have code goes in loop , sends emails off using function.
when there error not continue send emails after email had issue.
i error caught , system continue sending rest of emails. please see code below.
public static sendemailresult setemailandsend(datarow[] rows) { try { sendemailresult results = new sendemailresult(); foreach (datarow row in rows) { try { //set values here string strfn = row["firstname"].tostring(); string strln = row["lastname"].tostring(); string stremail = row["email"].tostring(); //some more here sent = emailfunctions.sendemail(stremail, strfromaddress, strfromname, strsubject, strfn + " " + strln, strbody, strsignoffembeddedimagepath, strsignoffembeddedimagename, strccreminderemail); } catch (exception exc) { //do stuff continue; } } } catch (exception exc) { //do } } public static bool sendemail(string strtoaddress, string strfromaddress, string strfromname, string strsubject, string strrecipientname, string strbody, string strembeddedimagepath, string strembeddedimagename, string strccreminderemail) { try { //smtpclient settings in webconfig smtpclient client = new smtpclient(); client.enablessl = true; using (mailmessage message = new mailmessage( new mailaddress(strfromaddress, strfromname), new mailaddress(strtoaddress, strrecipientname))) { message.isbodyhtml = true; message.subject = strsubject; message.body = strbody; if (!string.isnullorempty(strccreminderemail)) message.cc.add(strccreminderemail); client.send(message); return true; } } catch (exception ex) { throw; } }
you need move try catch
block inside foreach statement:
foreach (datarow row in rows) { try { //set values here string strfn = row["firstname"].tostring(); string strln = row["lastname"].tostring(); string stremail = row["email"].tostring(); //some more here sent = emailfunctions.sendemail(stremail, strfromaddress, strfromname, strsubject, strfn + " " + strln, strbody, strsignoffembeddedimagepath, strsignoffembeddedimagename, strccreminderemail); } catch (exception exc) { //log exception!! continue; } }
Comments
Post a Comment