c# - How to add multiple email address receipients retrieved from MySQL? -
i'm getting error on line below :-
email = mydatareader.getvalue(i).tostring();
what i'm trying retrieve multiple email addresses mysql , send email.
based on example have found, stores in arraylist, in case gives me error.
i have on google still not able correct error.
could 1 me out please ? thanks.
protected void searchemail () { mysqlconnection con = new mysqlconnection("server=localhost;userid=root;password=;database=obsystem"); con.open(); mysqlcommand cmd = new mysqlcommand("select cusemail,cusfname,newbalance monthlytracker month(paymentdate)='" + mm + "' , year(paymentdate)='" + year + "'and status='" + unpaid + "'",con); mysqldatareader mydatareader = cmd.executereader(); //arraylist list_emails = new arraylist(); int = 0; //string email = string.empty; list<custinfo> list_emails = new list<custinfo>(); custinfo customer; while (mydatareader.read()) { //list_emails.add(mydatareader.getvalue(i).tostring());//add array list //i = i++; //increment or ++i list_emails.add(new custinfo { email = mydatareader.getvalue(0).tostring(), name = mydatareader.getvalue(1).tostring(), balance = mydatareader.getvalue(2).tostring() }); } con.close(); //close connection foreach (custinfo cus in list_emails) { var fromaddress = new mailaddress("veolbakhda@gmail.com", "shilpesh"); var toaddress = new mailaddress(cus.email); const string frompassword = "xxxxxxxx"; string fullsubj = "payment reminder - madu d. trading (" + month + " , " + year + ")"; //const string subject = fullsubj; //const string body = "body"; string body1 = cus.name; string body2 = cus.balance; string bodyfull = body1 + body2; var smtp = new smtpclient { host = "smtp.gmail.com", port = 587, enablessl = true, deliverymethod = smtpdeliverymethod.network, usedefaultcredentials = false, credentials = new networkcredential(fromaddress.address, frompassword) }; using (var message = new mailmessage(fromaddress, toaddress) { subject = fullsubj, body = bodyfull }) { smtp.send(message); } } }
you declare variable email int
int = 0, email = 0;
and try store string:
email = mydatareader.getvalue(0).tostring();
declare variable email string:
string email = string.empty;
and don't need variable:
int = 0
and
i = + 1 - 1; //increment or ++i
can removed
edit after comment: create class customer information. don't know how fields called, cusname
, balance
, this:
public class custinfo { public string email {get; set;} public string name {get; set;} public string balance {get; set;} } protected void searchemail () { mysqlconnection con = new mysqlconnection("server=localhost;userid=root;password=;database=obsystem"); con.open(); mysqlcommand cmd = new mysqlcommand("select cusemail, cusname, balance monthlytracker , month(paymentdate)='" + mm + "' , year(paymentdate)='" + year + "'and status='" + unpaid + "'",con); mysqldatareader mydatareader = cmd.executereader(); list<custinfo> list_emails = new list<custinfo>(); custinfo customer; while (mydatareader.read()) { list_emails.add(new custinfo { email = mydatareader.getvalue(0).tostring(), name = mydatareader.getvalue(1).tostring(), balance = mydatareader.getvalue(2).tostring() }); } con.close(); //close connection foreach (custinfo customer in list_emails) { mailmessage mail = new mailmessage(); mail.to.add(customer.email); mail.subject = "welcome c#"; mail.from = new mailaddress(""); mail.body = "test"; // add values customer object mail => fe: mail.body.replace("$$name$$", customer.name); smtpclient smtp = new smtpclient("smtp server"); smtp.send(mail); } }
Comments
Post a Comment