C# 基于包含电子邮件地址的gridview列发送电子邮件

C# 基于包含电子邮件地址的gridview列发送电子邮件,c#,asp.net,visual-studio-2010,C#,Asp.net,Visual Studio 2010,我可以知道如何根据包含电子邮件地址的gridview列发送电子邮件吗? 我目前正在使用asp.net和c。我目前也在使用smtp gmail发送电子邮件 目前,我有一个gridview1,其中包含客户的电子邮件、姓名、帐户号,这些邮件已被支票拒付,但我希望在单击按钮后向所有这些客户发送一封标准电子邮件。我可以知道我该怎么做吗?他们的电子邮件存储在数据库中,并将显示在gridview上 private void SendEMail(MailMessage mail) { SmtpCl

我可以知道如何根据包含电子邮件地址的gridview列发送电子邮件吗? 我目前正在使用asp.net和c。我目前也在使用smtp gmail发送电子邮件

目前,我有一个gridview1,其中包含客户的电子邮件、姓名、帐户号,这些邮件已被支票拒付,但我希望在单击按钮后向所有这些客户发送一封标准电子邮件。我可以知道我该怎么做吗?他们的电子邮件存储在数据库中,并将显示在gridview上

   private void SendEMail(MailMessage mail)
{
    SmtpClient client = new SmtpClient();
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;
    client.Credentials = new System.Net.NetworkCredential("@gmail.com", "password");

    try
    {
        client.Send(mail);
    }
    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }

尝试将电子邮件地址保存在数组中

然后执行foreach并发送数组中的电子邮件

例如:

类别:

    public SmtpClient client = new SmtpClient();
    public MailMessage msg = new MailMessage();
    public System.Net.NetworkCredential smtpCreds = new System.Net.NetworkCredential("mail", "password");

public void Send(string sendTo, string sendFrom, string subject, string body)
{
    try
    {
        //setup SMTP Host Here
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.UseDefaultCredentials = false;
        client.Credentials = smtpCreds;
        client.EnableSsl = true;

        //convert string to MailAdress

        MailAddress to = new MailAddress(sendTo);
        MailAddress from = new MailAddress(sendFrom);

        //set up message settings

        msg.Subject = subject;
        msg.Body = body;
        msg.From = from;
        msg.To.Add(to);

        // Send E-mail

        client.Send(msg);

    }
    catch (Exception error)
    {
    }
}
发送电子邮件:点击按钮

//read the emails from the database and save them in an array.
//count how many are the emails, ex: int countEmails = SqlClass.Function("select emails from table").Rows.Count;

string[] emails = new string[countEmails];
foreach (string item in emails)
{
   //send e-mail
   callClass.Send(item, emailFrom, subject, body); //you can adapt the class
}

最简单的方法是遍历绑定到网格视图的数据集。但既然您已经询问了gridview,下面是如何在gridview行中循环

在按钮上单击“写入此内容”

string email = "";
foreach (GridViewRow item in GridView1.Rows)
{
      //considering 1st column contains email address
      //if not, replace it with correct index
      email =  item.Cells[0].Text;
      //code to send email

}
更新2

更新我的代码以使用sendEmail功能

public void button_click(object sender, EventArgs e)
{
    string email = "";
    foreach (GridViewRow item in GridView1.Rows)
    {

          //if not, replace it with correct index
          email =  item.Cells[4].Text;
          //code to send email
         //reciever email add
         MailAddress to = new MailAddress(email);
         //sender email address
         MailAddress from = new MailAddress("your@email");

        MailMessage msg = new MailMessage();
        //use reason shown in grid
        msg.Subject = item.Cells[3].Text;

        //you can similar extract FName, LName, Account number from grid
        // and use it in your message body
        //Keep your message body like 
        str email_msg = "Dear {0} {1}, Your cheque for account number {2} bounced because of    the reason {3}";
        msg = String.Format(email_msg , item.Cells[0].Text,item.Cells[1].Text,item.Cells[2].Text,item.Cells[3].Text);
        msg.Body = email_msg ;
        msg.From = from;
        msg.To.Add(to);

        SendEMail(msg);

    }
}

您能为网格视图和gmail设置提供一些代码示例吗?您好,我不太理解您对“发送电子邮件部分”的编码,例如SQLClass.Functionselect email from table.Rows.Count;我要为它做命令吗?像select语句吗?我还想知道“发送电子邮件”是否应该放在按钮点击中?这就是一个例子。你是怎么执行querys的?!