共计 3682 个字符,预计需要花费 10 分钟才能阅读完成。
一、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); // 参数 SendEmailID
comm.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
缺点,有人访问页面时,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].Rowsi.ToString();
string body =“你的网上注册失败,错误原因:”; body +=
ds.Tables[0].Rowsi.ToString() +“”; body +=“请查看资料修正”;
string SendEmailID = ds.Tables[0].Rowsi.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); // 参数 SendEmailID
comm.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; }
详细更多请关注 Kewail 官网:(www.kewail.com)