Code
1
2 protected override void OnStart(string[] args)
3 {
4 MyTimer();
5 }
6
7 //实例化System.Timers.Timer
8 private void MyTimer()
9 {
10 //设置时间间隔
11 System.Timers.Timer MT = new System.Timers.Timer(int.Parse(ConfigResource.Interval)*60*1000);
12 MT.Elapsed += new System.Timers.ElapsedEventHandler(MTimedEvent);
13 MT.Enabled = true;
14
15 }
16 //构造System.Timers.Timer实例 间隔时间事件 (定时执行事件)
17 private void MTimedEvent(object source, System.Timers.ElapsedEventArgs e)
18 {
19 //开始工作
20 StartWork();
21 }
22
23 public void StartWork()
24 {
25 //从数据库DB查询表A中的时间 代码省略。。。
26 //时间比较
27 if() //时间大于当前系统时间
28 {
29 //发送邮件
30 int iStatus = SendMail("你指定的收件人Email地址","标题","内容");
31 if( iStatus > 0)
32 {
33 using (StreamWriter sw = new StreamWriter(filePath + "log.txt", System.Text.Encoding.GetEncoding("utf-8")))
34 {
35 sw.Wirte(System.DateTime.Now.ToString() + " 发送邮件成功!")
36 }
37 }
38 else{//失败}
39 }
40 }
41
42 /// <summary>
43 /// 发送EMAIL
44 /// </summary>
45 /// <param name="sRecipientEmail">收件人地址</param>
46 /// <param name="sSubject">主题</param>
47 /// <param name="sMessage">内容</param>
48 /// <returns>发送是否成功</returns>
49 public bool SendMail(string sRecipientEmail, string sSubject, string sMessage)
50 {
51
52 //邮件对象
53 MailMessage emailMessage;
54
55 //smtp客户端对象
56
57 SmtpClient client;
58
59 // 初始化邮件对象
60
61 String sSenderEmail = "你的邮箱";
62
63 emailMessage = new MailMessage(sSenderEmail, sRecipientEmail, sSubject, sMessage);
64 emailMessage.IsBodyHtml = true;
65 emailMessage.SubjectEncoding = System.Text.Encoding.Default;
66 emailMessage.BodyEncoding = System.Text.Encoding.Default;
67 //加入
68 emailMessage.Headers.Add("X-Priority", "3");
69 emailMessage.Headers.Add("X-MSMail-Priority", "Normal");
70 emailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
71 emailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");
72
73 //邮件发送客户端
74 client = new SmtpClient();
75
76 //邮件服务器及帐户信息
77 client.Host = "邮件服务器";
78 //client.Host = "smtp.163.com";
79
80 //client.Port = 465;
81 //client.EnableSsl = true;
82 System.Net.NetworkCredential Credential = new System.Net.NetworkCredential();
83
84 Credential.UserName = "你的邮箱帐号" //可以在资源文件中配置
85 Credential.Password = "密码"
86
87 client.Credentials = Credential;
88
89 try
90 {
91 client.Send(emailMessage);
92 }
93 catch (Exception e)
94 {
95 return false;
96 }
97 return true;
98
99 }
100
101
102
1
2 protected override void OnStart(string[] args)
3 {
4 MyTimer();
5 }
6
7 //实例化System.Timers.Timer
8 private void MyTimer()
9 {
10 //设置时间间隔
11 System.Timers.Timer MT = new System.Timers.Timer(int.Parse(ConfigResource.Interval)*60*1000);
12 MT.Elapsed += new System.Timers.ElapsedEventHandler(MTimedEvent);
13 MT.Enabled = true;
14
15 }
16 //构造System.Timers.Timer实例 间隔时间事件 (定时执行事件)
17 private void MTimedEvent(object source, System.Timers.ElapsedEventArgs e)
18 {
19 //开始工作
20 StartWork();
21 }
22
23 public void StartWork()
24 {
25 //从数据库DB查询表A中的时间 代码省略。。。
26 //时间比较
27 if() //时间大于当前系统时间
28 {
29 //发送邮件
30 int iStatus = SendMail("你指定的收件人Email地址","标题","内容");
31 if( iStatus > 0)
32 {
33 using (StreamWriter sw = new StreamWriter(filePath + "log.txt", System.Text.Encoding.GetEncoding("utf-8")))
34 {
35 sw.Wirte(System.DateTime.Now.ToString() + " 发送邮件成功!")
36 }
37 }
38 else{//失败}
39 }
40 }
41
42 /// <summary>
43 /// 发送EMAIL
44 /// </summary>
45 /// <param name="sRecipientEmail">收件人地址</param>
46 /// <param name="sSubject">主题</param>
47 /// <param name="sMessage">内容</param>
48 /// <returns>发送是否成功</returns>
49 public bool SendMail(string sRecipientEmail, string sSubject, string sMessage)
50 {
51
52 //邮件对象
53 MailMessage emailMessage;
54
55 //smtp客户端对象
56
57 SmtpClient client;
58
59 // 初始化邮件对象
60
61 String sSenderEmail = "你的邮箱";
62
63 emailMessage = new MailMessage(sSenderEmail, sRecipientEmail, sSubject, sMessage);
64 emailMessage.IsBodyHtml = true;
65 emailMessage.SubjectEncoding = System.Text.Encoding.Default;
66 emailMessage.BodyEncoding = System.Text.Encoding.Default;
67 //加入
68 emailMessage.Headers.Add("X-Priority", "3");
69 emailMessage.Headers.Add("X-MSMail-Priority", "Normal");
70 emailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
71 emailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");
72
73 //邮件发送客户端
74 client = new SmtpClient();
75
76 //邮件服务器及帐户信息
77 client.Host = "邮件服务器";
78 //client.Host = "smtp.163.com";
79
80 //client.Port = 465;
81 //client.EnableSsl = true;
82 System.Net.NetworkCredential Credential = new System.Net.NetworkCredential();
83
84 Credential.UserName = "你的邮箱帐号" //可以在资源文件中配置
85 Credential.Password = "密码"
86
87 client.Credentials = Credential;
88
89 try
90 {
91 client.Send(emailMessage);
92 }
93 catch (Exception e)
94 {
95 return false;
96 }
97 return true;
98
99 }
100
101
102