Windows定时邮件服务笔记
来源:互联网 发布:四叶草引导mac和win7 编辑:程序博客网 时间:2024/06/06 16:47
-、创建项目
1)、创建项目类型为 Windows服务
2)、双击服务文件,于设计器处右键添加按装程序
系统自动添加2个安装组件(serviceProcessInstaller1、serviceInstaller1)
3)、serviceInstaller1 属性说明 ServiceName:为系统标识显示服务名称;StartType:为服务启动类型。serviceProcessInstaller1属性说明 Account:运行服务帐户类型
4)、书写代码
public partial class TimerService : ServiceBase { int hourTimeSpan = 0;//时间间隔 分钟 Timer timer1 = new Timer(); public FwFcTimerService() { InitializeComponent(); }
<span style="white-space:pre"></span>//服务启动执行事件 protected override void OnStart(string[] args) { try { hourTimeSpan = int.Parse(ToolHelper.ReadConfig("HourTimeSpan")); timer1.Interval = hourTimeSpan * 1000 * 60; //timer1.Interval = 10000; timer1.Elapsed += timer1_Elapsed; timer1.Enabled = true; timer1.Start(); } catch (Exception ex) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\TimerServiceLog.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + " 启动异常 " + ex.Message); } } } void timer1_Elapsed(object sender, ElapsedEventArgs e) { timer1.Stop(); try { //业务逻辑 } catch (Exception ex) { try { sendMail(content: DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "系统异常:" + ex.Message); } catch (Exception ex2) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\TimerServiceLog.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + " 邮件异常 " + ex2.Message); } } } timer1.Start(); }<span style="white-space:pre"></span>//服务停止执行事件 protected override void OnStop() { //sendMail(content: DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 系统异常:定时服务已停止!"); using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\TimerServiceLog.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") +" stop"); } } //发送邮件 private void sendMail(string title = "防伪防窜货定时服务程序异常", string content = "接口异常") { string[] SendMailInfo = ToolHelper.ReadConfig("SendMailInfo").Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); Email email = new Email(); email.mailFrom = SendMailInfo[0]; email.mailPwd = SendMailInfo[1]; email.host = SendMailInfo[2]; email.mailToArray = ToolHelper.ReadConfig("ReceiveMailInfo"); email.mailSubject = title; email.mailBody = content; email.isbodyHtml = false; bool isSend = email.Send(); } }
<pre name="code" class="csharp">/// <summary> /// 邮件 /// </summary> public class Email { /// <summary> /// 发送者 /// </summary> public string mailFrom { get; set; } /// <summary> /// 收件人 /// </summary> public string[] mailToArray { get; set; } /// <summary> /// 抄送 /// </summary> public string[] mailCcArray { get; set; } /// <summary> /// 标题 /// </summary> public string mailSubject { get; set; } /// <summary> /// 正文 /// </summary> public string mailBody { get; set; } /// <summary> /// 发件人密码 /// </summary> public string mailPwd { get; set; } /// <summary> /// SMTP邮件服务器 /// </summary> public string host { get; set; } /// <summary> /// 正文是否是html格式 /// </summary> public bool isbodyHtml { get; set; } /// <summary> /// 附件 /// </summary> public string[] attachmentsPath { get; set; } public bool Send() { //使用指定的邮件地址初始化MailAddress实例 MailAddress maddr = new MailAddress(mailFrom); //初始化MailMessage实例 MailMessage myMail = new MailMessage(); //向收件人地址集合添加邮件地址 if (mailToArray != null) { for (int i = 0; i < mailToArray.Length; i++) { myMail.To.Add(mailToArray[i].ToString()); } } //向抄送收件人地址集合添加邮件地址 if (mailCcArray != null) { for (int i = 0; i < mailCcArray.Length; i++) { myMail.CC.Add(mailCcArray[i].ToString()); } } //发件人地址 myMail.From = maddr; //电子邮件的标题 myMail.Subject = mailSubject; //电子邮件的主题内容使用的编码 myMail.SubjectEncoding = Encoding.UTF8; //电子邮件正文 myMail.Body = mailBody; //电子邮件正文的编码 myMail.BodyEncoding = Encoding.Default; myMail.Priority = MailPriority.High; myMail.IsBodyHtml = isbodyHtml; //在有附件的情况下添加附件 try { if (attachmentsPath != null && attachmentsPath.Length > 0) { Attachment attachFile = null; foreach (string path in attachmentsPath) { attachFile = new Attachment(path); myMail.Attachments.Add(attachFile); } } } catch (Exception err) { throw new Exception("在添加附件时有错误:" + err); } SmtpClient smtp = new SmtpClient(); //指定发件人的邮件地址和密码以验证发件人身份 smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd); //设置SMTP邮件服务器 smtp.Host = host; try { //将邮件发送到SMTP邮件服务器 smtp.Send(myMail); return true; } catch (System.Net.Mail.SmtpException ex) { return false; } } }
二、编写按装与卸载脚本
1)Install.bat 按装脚本文件 内容如下
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe CarlZeissJenaTimerService.exeNet Start CarlZeissJenaTimerServicesc config CarlZeissJenaTimerService start= auto
2)Uninstall.bat 卸载脚本文件 内容如下
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u CarlZeissJenaTimerService.exe三、调试服务程序
1)、创建调试项目(winform程序)
介面如下:
代码如下:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } //安装 private void button1_Click(object sender, EventArgs e) { string CurrentDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = CurrentDirectory + "\\Service"; Process process = new Process(); process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = "Install.bat"; process.StartInfo.CreateNoWindow = true; process.Start(); System.Environment.CurrentDirectory = CurrentDirectory; } //卸载 private void button2_Click(object sender, EventArgs e) { string CurrentDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = CurrentDirectory + "\\Service"; Process process = new Process(); process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = "Uninstall.bat"; process.StartInfo.CreateNoWindow = true; process.Start(); System.Environment.CurrentDirectory = CurrentDirectory; } //启动 private void button3_Click(object sender, EventArgs e) { ServiceController serviceController = new ServiceController("FwFcTimerService"); serviceController.Start(); } //停止 private void button4_Click(object sender, EventArgs e) { ServiceController serviceController = new ServiceController("FwFcTimerService"); if (serviceController.CanStop) serviceController.Stop(); } //暂停/继续 private void button5_Click(object sender, EventArgs e) { ServiceController serviceController = new ServiceController("FwFcTimerService"); if (serviceController.CanPauseAndContinue) { if (serviceController.Status == ServiceControllerStatus.Running) serviceController.Pause(); else if (serviceController.Status == ServiceControllerStatus.Paused) serviceController.Continue(); } } //检查状态 private void button6_Click(object sender, EventArgs e) { ServiceController serviceController = new ServiceController("FwFcTimerService"); string Status = serviceController.Status.ToString(); } }2)在调试项目下的bin\Debug目录下建立Service目录,并将windows服务生成目录设置为上面创建的Service目录。(通过项目属性修改生成目录地址)
3)启动调用项目程序、按装项目、启动服务、并附加服务进程序即可进行调试
0 0
- Windows定时邮件服务笔记
- windows服务定时发送邮件
- windows 定时发送邮件服务
- 定时启动Windows服务
- 编写一个Windows服务程序,定时从数据库中拿出记录发送邮件
- Windows服务程序,定时从数据库中拿出记录发送邮件
- 编写一个C#Windows服务程序,定时从数据库中拿出记录发送邮件。
- 使用QuartzNet和RabbitMQ实现定时发邮件的windows服务
- 使用windows服务+SQL存储过程实现某项信息定时自动发送邮件
- .net 定时执行 windows 服务
- python定时任务windows服务
- windows 定时服务之Timer
- 【Demo】Windows Service 定时服务
- ASP.NET定时发送邮件&windows server
- Linux学习笔记 --邮件服务
- Windows 定时执行 服务与任务计划
- 创建Windows服务程序实现定时操作
- C#创建windows服务并定时执行
- Xcode默认快捷键太别扭,一招教你制服它!
- 简单文本函数
- ubuntu下搭建samba服务,实现和window的文件共享
- apache+tomcat反向代理
- Ubuntu下安装Packet Tracer5.3过程
- Windows定时邮件服务笔记
- Atitit. 衡量项目规模 ----包含的类的数量 .net java类库包含多少类 多少个api方法??
- 使用未公开API ITrayNotify控制托盘图标的隐藏和显示
- SGU 194. Reactor Cooling【无源汇上下界最大流】
- 微博开发笔记上(未完待续)
- Collections分析之SynchronizedList
- Atitit.检测文本文件的编码 自动获取文件的中文编码
- 轮播图2(jQ)
- 浅谈Android中微信的文本,音频,视频,链接,表情,图片分享接口开发详解