一、sql触发器发送
二、winform程序后用任务计划发送
页面//引用using System.Data.Common;using System.Data.SqlClient;using System.Configuration;using System.Data;using System.Net.Mail;//要发送的邮件public static DataSet SendEmailList(){ DataSet ds = new DataSet();try{string str = ConfigurationSettings.AppSettings[“ConnectionString”]; SqlConnection conn = new SqlConnection(str); conn.Open(); SqlCommand comm = new SqlCommand(“SendEmailList”, conn); comm.CommandType = CommandType.StoredProcedure; comm.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = comm; da.Fill(ds); conn.Close(); }catch (Exception ex) { }return ds; }public static bool SendMail(string form, string toMail, string title, string body, string SendEmailID){bool result = false;try{ MailMessage email = new MailMessage(); email.From = new MailAddress(form);//收件人email.To.Add(toMail);//主题email.Subject = title;//内容email.Body = body;//优先级email.Priority = MailPriority.Normal;//内容类型email.IsBodyHtml = true; SmtpClient client = new SmtpClient(); client.Send(email);//再把状态改一下string str = ConfigurationSettings.AppSettings[“ConnectionString”].ToString(); SqlConnection conn = new SqlConnection(str); conn.Open(); SqlCommand comm = new SqlCommand(“SendEmailDelete”, conn);//参数SendEmailIDcomm.CommandType = CommandType.StoredProcedure; comm.Parameters.Add(new SqlParameter("@SendEmailID", SqlDbType.NVarChar, 50)); comm.Parameters["@SendEmailID"].Value = SendEmailID.ToString(); comm.ExecuteNonQuery(); result = true; }catch (Exception ex) { result = false; }return result; } webconfig页面配置 配置程序 开始----程序----附件—系统工具—任务计划,双击添加任务,选择程序文件-----(程序生成的*.exe),管理员输入密码为登录密码,一直点下一步就行了
三、global
Kewail平台免费测试 https://www.kewail.com/register.html?uid=1551347346028
缺点,有人访问页面时,global才会运行
webconfig页面配置
global页面void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码Timer t = new Timer(60000);//设计时间间隔,如果一个小时执行一次就改为3600000 ,这里一分钟调用一次t.Elapsed += new ElapsedEventHandler(t_Elapsed); t.AutoReset = true; t.Enabled = true; }private void t_Elapsed(object sender, ElapsedEventArgs e){ Response.Write(“执行成功”);//查询数据库DataSet ds = Common.SendEmailList();string FormEmail = ConfigurationManager.AppSettings[“UserEmail”];string title = “会员申请网上注册失败”;for (int i = 0; i < ds.Tables[0].Rows.Count; i++) {string ToEmail = ds.Tables[0].Rows[i][“EmailAddress”].ToString();string body = “你的网上注册失败,错误原因:”; body += ds.Tables[0].Rows[i][“Cause”].ToString() + “ ”; body += “请查看资料修正”;string SendEmailID = ds.Tables[0].Rows[i][“SendEmailID”].ToString(); Common.SendMail(FormEmail, ToEmail, title, body, SendEmailID); } } winform页面//引用using System.Data.Common;using System.Data.SqlClient;using System.Configuration;using System.Data;using System.Net.Mail;//要发送的邮件public static DataSet SendEmailList(){ DataSet ds = new DataSet();try{string str = ConfigurationManager.ConnectionStrings[“ConnectionString”].ConnectionString; SqlConnection conn = new SqlConnection(str); conn.Open(); SqlCommand comm = new SqlCommand(“SendEmailList”, conn); comm.CommandType = CommandType.StoredProcedure; comm.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = comm; da.Fill(ds); conn.Close(); }catch (Exception ex) { }return ds; }public static bool SendMail(string form, string toMail, string title, string body,string SendEmailID){bool result = false;try{ MailMessage email = new MailMessage(); email.From = new MailAddress(form);//收件人email.To.Add(toMail);//主题email.Subject = title;//内容email.Body = body;//优先级email.Priority = MailPriority.Normal;//内容类型email.IsBodyHtml = true; SmtpClient client = new SmtpClient(); client.Send(email);//再把状态改一下string str = ConfigurationManager.ConnectionStrings[“ConnectionString”].ConnectionString; SqlConnection conn = new SqlConnection(str); conn.Open(); SqlCommand comm = new SqlCommand(“SendEmailDelete”, conn);//参数SendEmailIDcomm.CommandType = CommandType.StoredProcedure; comm.Parameters.Add(new SqlParameter("@SendEmailID", SqlDbType.NVarChar, 50)); comm.Parameters["@SendEmailID"].Value = SendEmailID.ToString(); comm.ExecuteNonQuery(); result = true; }catch (Exception ex) { result = false; }return result; }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69926917/viewspace-2646681/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/69926917/viewspace-2646681/