【问题标题】:sending email based on a gridview column containing email addresses基于包含电子邮件地址的 gridview 列发送电子邮件
【发布时间】:2012-07-25 17:15:16
【问题描述】:

我可以知道如何根据包含电子邮件地址的 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);
    }

【问题讨论】:

  • 您能否为您的网格视图和 gmail 设置提供一些代码示例?

标签: c# asp.net visual-studio-2010


【解决方案1】:

最简单的方法是遍历绑定到网格视图的数据集。但是,既然您已经询问了 gridview,那么您可以通过以下方式循环遍历 gridview 行

在 button_click 上写这个

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

    }
}

【讨论】:

    【解决方案2】:

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

    然后执行一次 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)
        {
        }
    }
    

    发送电子邮件:(button_click)

    //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
    }
    

    【讨论】:

    • 嗨,我不太了解您的“发送电子邮件部分”的编码,例如 SQLClass.Function("select email from table").Rows.Count;我想为它做一个命令吗?像一个选择语句?我还可以知道“发送电子邮件”是否可以放在按钮单击中?
    • 这就是例子。你是如何执行查询的?!
    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 2014-07-20
    • 2017-03-01
    • 2015-04-18
    • 2016-01-21
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    相关资源
    最近更新 更多