通过电子邮件发送完整细节

时间:2015-05-12 06:55:13

标签: asp.net gridview

我想将学生详细信息发送到公司邮件ID。我在该网格视图中使用了网格视图,它将显示学生详细信息以及复选框。当我单击网格视图中的特定行时,该详细信息通过SMTP发送

protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("select email from details", con);
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {

                string email = rdr["email"].ToString();

                SendEmail(email);
            }
        }
    }
    private void SendEmail(string ToEmail)
    {
             string strEmail=string.Empty;
        MailMessage mailMessage = new MailMessage("raj@gmail.com", ToEmail);
        StringBuilder sbEmailBody = new StringBuilder();
        try
        {
            foreach (GridViewRow rw in GridView1.Rows)
            {
                CheckBox chkBx = (CheckBox)rw.FindControl("chk1");
                if (chkBx != null && chkBx.Checked)
                {
                    strEmail = ((Label)rw.FindControl("student Name")).Text;
                }
                sbEmailBody.Append(strEmail);
                    sbEmailBody.Append("");
                    sbEmailBody.Append("hello");
                    sbEmailBody.Append("<br/><br/>");
                    sbEmailBody.Append("<b>Testing Mail</b>");
                    mailMessage.IsBodyHtml = true;

                    mailMessage.Body = sbEmailBody.ToString();
                    mailMessage.Subject = "testing";
                    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);

                    smtpClient.Credentials = new System.Net.NetworkCredential()
                    {
                        UserName = "raj@gmail.com",
                        Password = "1234"
                    };

                    smtpClient.EnableSsl = true;
                    smtpClient.Send(mailMessage);
                }
            }

    catch
        {

        }
        }

         }

}

这里我无法从网格视图中发送学生的详细信息

1 个答案:

答案 0 :(得分:0)

由于您使用的是SSL,请尝试将SmtpClient的端口更改为465。

Google SMTP Configurations