网站中定时发送邮件
记录笔记:
类文件:
Code
public interface ISchedulerJob
{
void Execute();
}
/// <summary>
/// 定时任务实例
/// </summary>
public class SampleJob : ISchedulerJob
{
public static string BACKDOC_POLICY_ID = "920";
public void Execute()
{
try
{
//文件保存的物理路径,CSTest为虚拟目录名称,F:\SportsSln为物理路径
string p = "路径地址";
//我们在虚拟目录的根目录下建立SchedulerJob文件夹,并设置权限为匿名可修改
//Note.txt就是我们所写的文件
string FILE_NAME = p + "Note.txt";
string times = System.DateTime.Now.ToString("HH:mm");
string TIME_EQUALS = "23:30";
if (times.Equals(TIME_EQUALS))//如此定时,线程一分钟执行一次
{
#region 发送邮件 带附件
//收集数据 导出到TXT文件中:得到附件路径
string[] filesPaths = GetDataPaths();
string[] toAddresss = new string[] { "Email01@126.com", "Email02@163.com" };
//发送邮件 返回发送状态
string c = sendEmail(toAddresss, filesPaths);
#endregion
//标记是否是新建文件的标量
bool flag = false;
//如果文件不存在,就新建该文件
if (!File.Exists(FILE_NAME))
{
flag = true;
StreamWriter sr = File.CreateText(FILE_NAME);
sr.Close();
}
//向文件写入内容
StreamWriter x = new StreamWriter(FILE_NAME, true, System.Text.Encoding.Default);
if (flag) x.Write("计划任务测试开始:");
x.Write("\r\n邮件发送时间:" + c);
x.Close();
}
}
catch (Exception ex)
{
}
}
//收集数据
public string[] GetDataPaths()
{
string my_policy_ID = SampleJob.BACKDOC_POLICY_ID;
//System.Web.HttpContext.Current.Application[""]= "911";//insuranceNumber
try
{
SportsDb.DAL.RptDocDao obj = new RptDocDao();
//这里 3 是 后台管理员用户的ID
System.Data.DataSet ds = obj.ups_exportpolicydatatotxt(3, string.Format(" where条件", BACKDOC_POLICY_ID));
string policyinfo = "";
string insurantinfo = "";
if (ds.Tables[0].Rows.Count > 0)
{
string policyinfoITEM = ds.Tables[0].Rows[0]["policyinfo"].ToString().Trim();
SampleJob.BACKDOC_POLICY_ID = policyinfoITEM.Split('|')[2];//标识列 最大的保单ID
}
foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
{
policyinfo += Convert.ToString(dr["policyinfo"]) + "\r\n";
}
foreach (System.Data.DataRow dr in ds.Tables[1].Rows)
{
insurantinfo += Convert.ToString(dr["insurantinfo"]) + "\r\n";
}
//生成的文件先保存至服务器下载文件夹中 生成文件名: policy + 日期 + .txt info + 日期+.txt
string policyfile = @"投保单信息" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt";
string infofile = @"被保险人信息" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt";
string path = System.AppDomain.CurrentDomain.BaseDirectory + @"\ExportedFiles" + @"\";
byte[] data0 = System.Text.Encoding.GetEncoding("gb2312").GetBytes(policyinfo);
string file0 = SaveFileByFileStrem(@"" + path + policyfile, data0);
byte[] data1 = System.Text.Encoding.GetEncoding("gb2312").GetBytes(insurantinfo);
string file1 = SaveFileByFileStrem(@"" + path + infofile, data1);
return new string[] { file0, file1 };
}
catch (Exception ex)
{
return null;
}
}
//文件流保存为文件,返回文件路径
public string SaveFileByFileStrem(string filename, byte[] filecontent)
{//
string path = filename;
if (string.IsNullOrEmpty(path))
{
return "";
}
File.WriteAllBytes(path, filecontent);
return path;
}
//发送邮件
public string sendEmail(string[] toAddresss,string[] filesPaths)
{
if (MailDll.leiSendEmail.sendEmail(toAddresss,filesPaths))
{
return "^p^ 邮件发送成功!!!!!时间:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
return "-@- 邮件发送失败!!!!时间:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}
public class SchedulerConfiguration
{
private int sleepInterval;//任务列表
private ArrayList jobs = new ArrayList();
public int SleepInterval
{
get { return sleepInterval; }
}
public ArrayList Jobs
{
get { return jobs; }
}
//调度配置类的构造函数
public SchedulerConfiguration(int newSleepInterval)
{
sleepInterval = newSleepInterval * 1000;
}
}
public class Scheduler
{
private SchedulerConfiguration configuration = null;
public Scheduler(SchedulerConfiguration config)
{
configuration = config;
}
public void Start()
{
while (true)
{
//执行每一个任务
foreach (ISchedulerJob job in configuration.Jobs)
{
ThreadStart myThreadDelegate = new ThreadStart(job.Execute);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();
Thread.Sleep(configuration.SleepInterval);
}
}
}
}
public interface ISchedulerJob
{
void Execute();
}
/// <summary>
/// 定时任务实例
/// </summary>
public class SampleJob : ISchedulerJob
{
public static string BACKDOC_POLICY_ID = "920";
public void Execute()
{
try
{
//文件保存的物理路径,CSTest为虚拟目录名称,F:\SportsSln为物理路径
string p = "路径地址";
//我们在虚拟目录的根目录下建立SchedulerJob文件夹,并设置权限为匿名可修改
//Note.txt就是我们所写的文件
string FILE_NAME = p + "Note.txt";
string times = System.DateTime.Now.ToString("HH:mm");
string TIME_EQUALS = "23:30";
if (times.Equals(TIME_EQUALS))//如此定时,线程一分钟执行一次
{
#region 发送邮件 带附件
//收集数据 导出到TXT文件中:得到附件路径
string[] filesPaths = GetDataPaths();
string[] toAddresss = new string[] { "Email01@126.com", "Email02@163.com" };
//发送邮件 返回发送状态
string c = sendEmail(toAddresss, filesPaths);
#endregion
//标记是否是新建文件的标量
bool flag = false;
//如果文件不存在,就新建该文件
if (!File.Exists(FILE_NAME))
{
flag = true;
StreamWriter sr = File.CreateText(FILE_NAME);
sr.Close();
}
//向文件写入内容
StreamWriter x = new StreamWriter(FILE_NAME, true, System.Text.Encoding.Default);
if (flag) x.Write("计划任务测试开始:");
x.Write("\r\n邮件发送时间:" + c);
x.Close();
}
}
catch (Exception ex)
{
}
}
//收集数据
public string[] GetDataPaths()
{
string my_policy_ID = SampleJob.BACKDOC_POLICY_ID;
//System.Web.HttpContext.Current.Application[""]= "911";//insuranceNumber
try
{
SportsDb.DAL.RptDocDao obj = new RptDocDao();
//这里 3 是 后台管理员用户的ID
System.Data.DataSet ds = obj.ups_exportpolicydatatotxt(3, string.Format(" where条件", BACKDOC_POLICY_ID));
string policyinfo = "";
string insurantinfo = "";
if (ds.Tables[0].Rows.Count > 0)
{
string policyinfoITEM = ds.Tables[0].Rows[0]["policyinfo"].ToString().Trim();
SampleJob.BACKDOC_POLICY_ID = policyinfoITEM.Split('|')[2];//标识列 最大的保单ID
}
foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
{
policyinfo += Convert.ToString(dr["policyinfo"]) + "\r\n";
}
foreach (System.Data.DataRow dr in ds.Tables[1].Rows)
{
insurantinfo += Convert.ToString(dr["insurantinfo"]) + "\r\n";
}
//生成的文件先保存至服务器下载文件夹中 生成文件名: policy + 日期 + .txt info + 日期+.txt
string policyfile = @"投保单信息" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt";
string infofile = @"被保险人信息" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt";
string path = System.AppDomain.CurrentDomain.BaseDirectory + @"\ExportedFiles" + @"\";
byte[] data0 = System.Text.Encoding.GetEncoding("gb2312").GetBytes(policyinfo);
string file0 = SaveFileByFileStrem(@"" + path + policyfile, data0);
byte[] data1 = System.Text.Encoding.GetEncoding("gb2312").GetBytes(insurantinfo);
string file1 = SaveFileByFileStrem(@"" + path + infofile, data1);
return new string[] { file0, file1 };
}
catch (Exception ex)
{
return null;
}
}
//文件流保存为文件,返回文件路径
public string SaveFileByFileStrem(string filename, byte[] filecontent)
{//
string path = filename;
if (string.IsNullOrEmpty(path))
{
return "";
}
File.WriteAllBytes(path, filecontent);
return path;
}
//发送邮件
public string sendEmail(string[] toAddresss,string[] filesPaths)
{
if (MailDll.leiSendEmail.sendEmail(toAddresss,filesPaths))
{
return "^p^ 邮件发送成功!!!!!时间:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
return "-@- 邮件发送失败!!!!时间:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}
public class SchedulerConfiguration
{
private int sleepInterval;//任务列表
private ArrayList jobs = new ArrayList();
public int SleepInterval
{
get { return sleepInterval; }
}
public ArrayList Jobs
{
get { return jobs; }
}
//调度配置类的构造函数
public SchedulerConfiguration(int newSleepInterval)
{
sleepInterval = newSleepInterval * 1000;
}
}
public class Scheduler
{
private SchedulerConfiguration configuration = null;
public Scheduler(SchedulerConfiguration config)
{
configuration = config;
}
public void Start()
{
while (true)
{
//执行每一个任务
foreach (ISchedulerJob job in configuration.Jobs)
{
ThreadStart myThreadDelegate = new ThreadStart(job.Execute);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();
Thread.Sleep(configuration.SleepInterval);
}
}
}
}
global.asax文件:
Code
//定义线程变量
public System.Threading.Thread schedulerThread = null;
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
//Application["BACKDOC_POLICY_ID"] = "911";
//实例化调度配置
SportsDb.DAL.SchedulerConfiguration config = new SportsDb.DAL.SchedulerConfiguration(60);//一分钟执行一次
//添加任务
config.Jobs.Add(new SportsDb.DAL.SampleJob());
SportsDb.DAL.Scheduler scheduler = new SportsDb.DAL.Scheduler(config);
//创建 ThreadStart 委托
System.Threading.ThreadStart myThreadStart = new System.Threading.ThreadStart(scheduler.Start);
//实例化线程
System.Threading.Thread schedulerThread = new System.Threading.Thread(myThreadStart);
//启动线程
schedulerThread.Start();
}
void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码
if (null != schedulerThread)
{
schedulerThread.Abort();
}
}
//Exception LastEX = System.Web.HttpContext.Current.Server.GetLastError();
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
//Response.Write(LastEX.Message.ToString());
}
//定义线程变量
public System.Threading.Thread schedulerThread = null;
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
//Application["BACKDOC_POLICY_ID"] = "911";
//实例化调度配置
SportsDb.DAL.SchedulerConfiguration config = new SportsDb.DAL.SchedulerConfiguration(60);//一分钟执行一次
//添加任务
config.Jobs.Add(new SportsDb.DAL.SampleJob());
SportsDb.DAL.Scheduler scheduler = new SportsDb.DAL.Scheduler(config);
//创建 ThreadStart 委托
System.Threading.ThreadStart myThreadStart = new System.Threading.ThreadStart(scheduler.Start);
//实例化线程
System.Threading.Thread schedulerThread = new System.Threading.Thread(myThreadStart);
//启动线程
schedulerThread.Start();
}
void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码
if (null != schedulerThread)
{
schedulerThread.Abort();
}
}
//Exception LastEX = System.Web.HttpContext.Current.Server.GetLastError();
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
//Response.Write(LastEX.Message.ToString());
}