How to build list of failed emails in foreach loop in C# -
i trying check email validation list of mails. if of emails invalid, have send list of failed mail validations false message, new c#, how can achieve this? when reading books found possible through stringbuilder, unable find syntax this, can please provide syntax this.
public static string issmtprequestvalid(this smtpgatewayrequest smtprequest) { smtprequestcontent content = smtprequest.body smtprequestcontent; foreach (string email in content.emailcc) { bool valid = isemail(email); /* here check in of mails invalid have return false, list of invalid mails.*/ } }
thanks lot in advance.
first have create instance of stringbuilder class , append invalid emails in new lines. if want return emails in string have call .tostring()
method.
public static string issmtprequestvalid(this smtpgatewayrequest smtprequest) { stringbuilder invalidemails = new stringbuilder(); smtprequestcontent content = smtprequest.body smtprequestcontent; content.emailcc.where(email => !isemail(email)) .foreach(n => { invalidemails.appendline(n) }); return invalidemails.tostring(); }
or in easier-to-understand form
public static string issmtprequestvalid(this smtpgatewayrequest smtprequest) { stringbuilder invalidemails = new stringbuilder(); smtprequestcontent content = smtprequest.body smtprequestcontent; foreach (var email in content.emailcc) { if (!isemail(email)) { invalidemails.appendline(email); } } return invalidemails.tostring(); }
moving away solutions think best solution return entire list of items. example:
public static list<string> issmtprequestvalid(this smtpgatewayrequest smtprequest) { list<string> invalidemails = new list<string>(); smtprequestcontent content = smtprequest.body smtprequestcontent; foreach (var email in content.emailcc) { if (!isemail(email)) { invalidemails.add(email); } } return invalidemails; }
Comments
Post a Comment