1

我在下面的这一行收到错误:

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);
        }
    }
}
4

2 回答 2

1

您将变量 email 声明为 int

int i = 0, email = 0;

然后你尝试存储一个字符串:

email = myDataReader.GetValue(0).ToString();

将变量 email 声明为字符串:

string email = string.Empty;

你不需要 i 变量:

int i = 0

i = i + 1 - 1; //increment or ++i 

可以删除

评论后编辑:您为客户信息创建一个类。我不知道你的字段是如何被调用的,但是说它们是cusNameand balance,你会做这样的事情:

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 from monthlytracker AND MONTH(paymentDate)='" + mm + "' AND 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 to C#";

        mail.From = new MailAddress("");

        mail.Body = "Test";

        // add the values from the customer object to your mail => fe: mail.Body.Replace("$$name$$", customer.Name);

        SmtpClient smtp = new SmtpClient("SMTP Server");

        smtp.Send(mail);

 }
}
于 2013-08-10T07:33:56.150 回答
0

你的代码应该是

 while (myDataReader.Read())
    {

        list_emails.Add(myDataReader.GetValue(0).ToString());//add to array list            
    }
于 2013-08-10T07:39:28.280 回答