我打算只从 SQLdatabase 中检索列数据,并将其存储在字符串或标签中作为持有者。
案例: 从数据库 "tblUser" WHERE apple = 1 中选择所有电子邮件地址并存储在 "emaillist" 字符串持有者中,然后附加 mail.to.add(emaillist);
注意: ExecuteScalar 只允许我检索数据库的第一行。因此,只有第一行电子邮件地址能够检索。(例如,user@user.com)
预期结果: 由“user@user.com,testing.gmail.com,admin@admin.com”等组成的电子邮件列表。
我的代码:
String status = "1";
SqlConnection conn100 = new SqlConnection("Server=localhost;Integrated Security=true;database=WebsiteDatabase");
conn100.Open();
SqlCommand cmd100 = conn100.CreateCommand();
cmd100.CommandText = "select emailaddress from tblUser where apple= '" + status + "'";
string selected100 = cmd100.ExecuteScalar().ToString();
String emailcontent = "";
emailcontent = "" + selected1 + "- Highlight: " + selected2;
String emaillist = "" + selected100;
//Create a new MailMessage in order to send email
MailMessage mail = new MailMessage();
//The recipient of this email
mail.To.Add("recipient@gmail.com" + emaillist);
//The sender of this email
mail.From = new MailAddress("sender@gmail.com");
//The subject of this email
mail.Subject = "Latest Product";
//The email's content
string body = "New Product added! " + emailcontent;
mail.Body = body;
mail.IsBodyHtml = true;
//Create new smtpClient and use smtp services
SmtpClient smtp = new SmtpClient();
//SMTP host (Gmail)
smtp.Host = "smtp.gmail.com";
//SMTP port
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
//Sender Email and password
smtp.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "password");
//Enable SSL in order to have secure transmission
smtp.EnableSsl = true;
//Send the email
smtp.Send(mail);