网站自动发送邮件代码
一、sql触发器发送
二、winform程序后用任务计划发送
页面
//引用usingSystem.Data.Common;usingSystem.Data.SqlClient;usingSystem.Configuration;usingSystem.Data;usingSystem.Net.Mail;//要发送的邮件publicstaticDataSetSendEmailList(){DataSet ds =newDataSet();try{stringstr = ConfigurationSettings.AppSettings[“ConnectionString”];SqlConnection conn =newSqlConnection(str);conn.Open();SqlCommand comm =newSqlCommand(“SendEmailList”, conn);comm.CommandType = CommandType.StoredProcedure;comm.ExecuteNonQuery();SqlDataAdapter da =newSqlDataAdapter();da.SelectCommand = comm;da.Fill(ds);conn.Close();}catch(Exception ex) { }returnds;}publicstaticboolSendMail(stringform,stringtoMail,stringtitle,stringbody,stringSendEmailID){boolresult =false;try{MailMessage email =newMailMessage();email.From =newMailAddress(form);//收件人email.To.Add(toMail);//主题email.Subject = title;//内容email.Body = body;//优先级email.Priority = MailPriority.Normal;//内容类型email.IsBodyHtml =true;SmtpClient client =newSmtpClient();client.Send(email);//再把状态改一下stringstr = ConfigurationSettings.AppSettings[“ConnectionString”].ToString();SqlConnection conn =newSqlConnection(str);conn.Open();SqlCommand comm =newSqlCommand(“SendEmailDelete”, conn);//参数SendEmailIDcomm.CommandType = CommandType.StoredProcedure;comm.Parameters.Add(newSqlParameter("@SendEmailID", SqlDbType.NVarChar,50));comm.Parameters["@SendEmailID"].Value = SendEmailID.ToString();comm.ExecuteNonQuery();result =true;}catch(Exception ex){result =false;}returnresult;}webconfig页面配置配置程序开始----程序----附件—系统工具—任务计划,双击添加任务,选择程序文件-----(程序生成的*.exe),管理员输入密码为登录密码,一直点下一步就行了
三、global
缺点,有人访问页面时,global才会运行
webconfig页面配置global页面voidApplication_Start(objectsender, EventArgs e){// 在应用程序启动时运行的代码Timer t =newTimer(60000);//设计时间间隔,如果一个小时执行一次就改为3600000 ,这里一分钟调用一次t.Elapsed +=newElapsedEventHandler(t_Elapsed);t.AutoReset =true;t.Enabled =true;}privatevoidt_Elapsed(objectsender, ElapsedEventArgs e){Response.Write(“执行成功”);//查询数据库DataSet ds = Common.SendEmailList();stringFormEmail = ConfigurationManager.AppSettings[“UserEmail”];stringtitle = “会员申请网上注册失败”;for(inti =0; i < ds.Tables[0].Rows.Count; i++){stringToEmail = ds.Tables[0].Rows[i][“EmailAddress”].ToString();stringbody = “你的网上注册失败,错误原因:”;body += ds.Tables[0].Rows[i][“Cause”].ToString() + “”;body += “请查看资料修正”;stringSendEmailID = ds.Tables[0].Rows[i][“SendEmailID”].ToString();Common.SendMail(FormEmail, ToEmail, title, body, SendEmailID);}}winform页面//引用usingSystem.Data.Common;usingSystem.Data.SqlClient;usingSystem.Configuration;usingSystem.Data;usingSystem.Net.Mail;//要发送的邮件publicstaticDataSetSendEmailList(){DataSet ds =newDataSet();try{stringstr = ConfigurationManager.ConnectionStrings[“ConnectionString”].ConnectionString;SqlConnection conn =newSqlConnection(str);conn.Open();SqlCommand comm =newSqlCommand(“SendEmailList”, conn);comm.CommandType = CommandType.StoredProcedure;comm.ExecuteNonQuery();SqlDataAdapter da =newSqlDataAdapter();da.SelectCommand = comm;da.Fill(ds);conn.Close();}catch(Exception ex) { }returnds;}publicstaticboolSendMail(stringform,stringtoMail,stringtitle,stringbody,stringSendEmailID){boolresult =false;try{MailMessage email =newMailMessage();email.From =newMailAddress(form);//收件人email.To.Add(toMail);//主题email.Subject = title;//内容email.Body = body;//优先级email.Priority = MailPriority.Normal;//内容类型email.IsBodyHtml =true;SmtpClient client =newSmtpClient();client.Send(email);//再把状态改一下stringstr = ConfigurationManager.ConnectionStrings[“ConnectionString”].ConnectionString;SqlConnection conn =newSqlConnection(str);conn.Open();SqlCommand comm =newSqlCommand(“SendEmailDelete”, conn);//参数SendEmailIDcomm.CommandType = CommandType.StoredProcedure;comm.Parameters.Add(newSqlParameter("@SendEmailID", SqlDbType.NVarChar,50));comm.Parameters["@SendEmailID"].Value = SendEmailID.ToString();comm.ExecuteNonQuery();result =true;}catch(Exception ex){result =false;}returnresult;}
详细更多请关注Kewail官网:(www.kewail.com)