0

我正在尝试发送我的 GridView 中列出的所有电子邮件,但由于某种原因,电子邮件没有发送出去。我怀疑我的发送功能 (smtpClient.Send(mailMessage); 不工作或我遗漏了一些东西。请帮忙,因为我花了很多时间来解决这个问题。谢谢

protected void chkAll_CheckedChanged(object sender, EventArgs e) 
{ 
    foreach(GridViewRow gr in GridView1.Rows) 
    { 
        CheckBox cb = (CheckBox)gr.FindControl("chkItem"); 
        if(((CheckBox)sender).Checked) 
            cb.Checked = true; 
        else 
             cb.Checked = false; 
    } 
} 

protected void Button3_Click(object sender, EventArgs e) 
{ 
    StringBuilder sb = new StringBuilder(); 

    foreach(GridViewRow gr in GridView1.Rows) 
    { 
        CheckBox cb = (CheckBox)gr.FindControl("chkItem"); 
        if(cb.Checked) 
        { 
            sb.Append(GridView1.DataKeys[gr.RowIndex]["Email"].ToString()); 
            sb.Append(","); 
        } 
    } 

    //Create instance of main mail message class. 
    System.Net.Mail.MailMessage mailMessage=new System.Net.Mail.MailMessage(); 

        mailMessage.From = new System.Net.Mail.MailAddress(
                   System.Configuration.ConfigurationManager
                                 .AppSettings["fromEmailAddress"]); 

        mailMessage.Priority = System.Net.Mail.MailPriority.High; 
        //Text/HTML 
        mailMessage.IsBodyHtml = false; 
        mailMessage.Body = "Hello, here is new email"; 
        mailMessage.Subject = "RCA APPROVAL IS REQUIRED"; 

        System.Net.Mail.SmtpClient smtpClient=new System.Net.Mail.SmtpClient();

        try 
        { 
            smtpClient.Send(mailMessage); 

            Response.Write("<B>Email Has been sent successfully.</B>"); 
        } 
        catch (Exception ex) 
        { 
            Response.Write(ex.Message); 
        } 
    }
4

2 回答 2

2

您需要设置To地址MailMessage

你可能想这样做:

protected void Button3_Click(object sender, EventArgs e) 
{
    MailMessage mailMessage=new MailMessage(); 

    foreach(GridViewRow gr in GridView1.Rows) 
    { 
        CheckBox cb = (CheckBox)gr.FindControl("chkItem"); 
        if(cb.Checked) 
        { 
            //sb.Append(GridView1.DataKeys[gr.RowIndex]["Email"].ToString()); 
            //sb.Append(","); 

            //SETUP THE EMAIL ADDRESSES TO WHICH YOU WANT TO SEND EMAIL
            mailMessage.To.Add(new MailAddress(
                     GridView1.DataKeys[gr.RowIndex]["Email"].ToString()));
        } 
    } 

此外,如果您的SMTP服务器需要身份验证,您还需要提供Credentials。或者您可以将所有设置放入web.config中的<system.net><mailSettings>标记中,如此处所示,这正是强大的Scott Gu

于 2012-10-24T01:50:33.017 回答
0

这是因为您没有将电子邮件发送给任何人。

使用网格视图上的选定地址设置 mailMessage.To 属性的值。

此外,请检查您的 SMTP 服务,可能需要提供凭据并启用 SSL。

于 2012-10-24T01:57:21.713 回答