郵件接口的三種發送方式 1、sql觸發器發送web
2、winform程序後用任務計劃發送sql
頁面數據庫
//引用 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;orm
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();string
result = true; } catch (Exception ex) { result = false; }it
return result; }io
webconfig頁面配置
配置程序
開始----程序----附件—系統工具—任務計劃,雙擊添加任務,選擇程序文件-----(程序生成的*.exe),管理員輸入密碼爲登陸密碼,一直點下一步就好了 3、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].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); //參數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)