我是想在删除按钮执行删除操作时,同时发送邮件给管理员.
我在dataview中添加了个删除摸板列,事件代码如下:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
HttpCookie cookie1 = Request.Cookies["user"];
string userName = cookie1.Values["userName"].ToString();
string adID = cookie1.Values["adID"].ToString();
SqlConnection con = DB.CreateDB();
con.Open();
string title = ((Label)this.GridView1.Rows[e.RowIndex].Cells[1].FindControl("lbl_title")).Text;
string input_time = ((Label)this.GridView1.Rows[e.RowIndex].Cells[4].FindControl("lbl_input_time")).Text;
SqlCommand cmd = new SqlCommand("select coursePath from courseWare where courseTitle='" + title + "'and input_time='" + input_time + "'", con);
try
{
string coursePath = Convert.ToString(cmd.ExecuteScalar());
//string Course = coursePath.Substring(coursePath.LastIndexOf("/") + 1);
cmd.CommandText = "delete from courseWare where courseTitle='" + title + "'and input_time='" + input_time + "'";
cmd.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('删除课件成功!');</script>");
//发送邮件给管理员
string fromMail = "*****@126.com";
string toMail = "*****@126.com";
MailMessage mm = new MailMessage(fromMail, toMail);
mm.Subject = "彻底删除课件请求";
mm.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
mm.Body = "尊敬的系统管理员:" + "<br>" + "您好!" + "<br>" + "用户" + userName + "提出删除课件的请求,课件的路径及名称为:" + coursePath + ",收到此请求后请进行服务器相应课件的清理." + "<br>" + "请重新登录来验证您操作的正确性,如果还有问题应及时与系统管理员联系,谢谢您的支持和使用!" + "<br>" + "系统管理员的E-mail:air_wf@126.com";
mm.IsBodyHtml = true;
mm.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
SmtpClient sc = new SmtpClient();
try
{
sc.Send(mm);
}
catch (Exception ex)
{
if (ex is SmtpException)
{
Response.Write("<script language=javascript>alert('删除课件请求邮件发送失败,请及时与管理员联系!');</script>");
}
else
{
Response.Write(e.ToString());
}
}
}
catch (Exception ex)
{
Response.Write("<script language=javascript>alert('删除课件失败,有疑问请联系管理员!');</script>");
}
finally
{
con.Close();
}
}
麻烦大家给我支支招,谢谢