如何添加从MySQL检索到的多个电子邮件地址收件人?

时间:2013-08-10 07:28:56

标签: c# asp.net arrays email

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

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

2 个答案:

答案 0 :(得分:1)

您将变量email声明为int

int i = 0, email = 0;

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

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

将变量email声明为string:

string email = string.Empty;

并且您不需要i变量:

int i = 0

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

可以删除

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

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

 }
}

答案 1 :(得分:0)

你的代码应该是

 while (myDataReader.Read())
    {

        list_emails.Add(myDataReader.GetValue(0).ToString());//add to array list            
    }