我在下面的这一行收到错误:
email = myDataReader.GetValue(i).ToString();
我想做的只是从 MySQL 中检索多个电子邮件地址并发送电子邮件。
根据我找到的示例,它存储在 arraylist 中,但在我的情况下它给了我错误。
protected void searchEmail ()
{
MySqlConnection con = new MySqlConnection("server=localhost;userid=root;password=;database=obsystem");
con.Open();
MySqlCommand cmd = new MySqlCommand("SELECT cusEmail,cusFName,newBalance FROM monthlytracker WHERE MONTH(paymentDate)='" + mm + "' AND YEAR(paymentDate)='" + year + "'AND status='" + Unpaid + "'",con);
MySqlDataReader myDataReader = cmd.ExecuteReader();
//ArrayList list_emails = new ArrayList();
int i = 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 to 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);
}
}
}