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

时间:2022-06-11 15:14:56

I'm getting an error on this line below :-

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

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

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

What I'm trying to do is just retrieve multiple email addresses from MySQL and send email.

我想要做的只是从MySQL检索多个电子邮件地址并发送电子邮件。

Based on the example that I have found, it stores in the arraylist, but in my case it gives me error.

基于我找到的示例,它存储在arraylist中,但在我的情况下,它给了我错误。

I have look on the Google but still not able to correct the error.

我查看了Google但仍然无法纠正错误。

Could some one help 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 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 个解决方案

#1


1  

You declare the variable email as int

您将变量email声明为int

int i = 0, email = 0;

and then you try to store a string:

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

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

Declare the variable email as string:

将变量email声明为字符串:

string email = string.Empty;

and you don't need the i variable:

而且你不需要i变量:

int i = 0

and

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

can be removed

可以删除

Edit after comment: You create a class for the customer information. I don't know how your fields are called, but say they are cusName and balance, you would do something like this:

评论后编辑:您为客户信息创建一个类。我不知道你的字段是如何调用的,但是说它们是cusName和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);

 }
}

#2


0  

your code should be

你的代码应该是

 while (myDataReader.Read())
    {

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

#1


1  

You declare the variable email as int

您将变量email声明为int

int i = 0, email = 0;

and then you try to store a string:

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

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

Declare the variable email as string:

将变量email声明为字符串:

string email = string.Empty;

and you don't need the i variable:

而且你不需要i变量:

int i = 0

and

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

can be removed

可以删除

Edit after comment: You create a class for the customer information. I don't know how your fields are called, but say they are cusName and balance, you would do something like this:

评论后编辑:您为客户信息创建一个类。我不知道你的字段是如何调用的,但是说它们是cusName和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);

 }
}

#2


0  

your code should be

你的代码应该是

 while (myDataReader.Read())
    {

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