2012-07-25 178 views
0

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

目前,我有一个gridview1包含客户(电子邮件,名称,accountNo)已经退回支票,但我希望发送一个标准的电子邮件,所有这些客户点击一个按钮。我可以知道该怎么办?他们的电子邮件存储在数据库中,并将显示在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); 
    } 

enter image description here

+0

你能提供一些代码示例为你网格视图和gmail设置? – MikeTWebb 2012-07-25 17:19:00

回答

0

最简单的是通过你已经绑定到网格视图的数据集循环。但既然你问到的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("[email protected]"); 

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

    } 
} 
+0

谢谢!它正在工作。 – KYQ 2012-07-25 18:23:37

0

尝试保存在阵列中的电子邮件地址。

然后做一个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 
} 
+0

嗨,我真的不理解你的'发送电子邮件部分'的编码,如SQLClass.Function(“从表中选择电子邮件”)。Rows.Count; 我想为它做一个命令吗?像select语句一样? 我可否也知道'发送电子邮件'是否可以放入按钮中? – KYQ 2012-07-25 17:57:35

+0

就是这样的例子。 你如何执行查询? – Severiano 2012-07-25 18:02:40

相关问题