当前位置 : 主页 > 网络安全 > 测试自动化 >

自动化发送邮件的三种方式

来源:互联网 收集:自由互联 发布时间:2021-06-19
一、sql触发器发送 二、winform程序后用任务计划发送 页面//引用using System.Data.Common;using System.Data.SqlClient;using System.Configuration;using System.Data;using System.Net.Mail;//要发送的邮件public static Dat

一、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].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)

网友评论