windows service(windows 服务程序应用于自动发送邮件)
2009-11-03 19:36
666 查看
所谓Windows后台服务,即后台自动运行的程序,一般随操作系统启动而启动,在我的电脑 服务后应用程序 服务里面能看到当前电脑的服务.一般而言,程序上用VC、C++写Windows服务,但是我对这些语言不是很熟,一般编程用C#较多,所以就用C#语言写了一个Windows服务.
什么是Windows服务?
Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合。它没有用户界面,并且也不会产生任何可视输出。任何用户消息都会被写进Windows事件日志。计算机启动时,服务会自动开始运行。它们不要用户一定登录才运行,它们能在包括这个系统内的任何用户环境下运行。通过服务控制管理器,Windows服务是可控的,可以终止、暂停及当需要时启动。
Windows 服务,以前的NT服务,都是被作为Windows NT操作系统的一部分引进来的。它们在Windows 9x及Windows Me下没有。你需要使用NT级别的操作系统来运行Windows服务,诸如:Windows NT、Windows 2000 Professional或Windows 2000 Server。举例而言,以Windows服务形式的产品有:Microsoft Exchange、SQL Server,还有别的如设置计算机时钟的Windows Time服务。
创建一个Windows服务
我们即将创建的这个服务除了演示什么也不做。服务被启动时会把一个条目信息登记到一个数据库当中来指明这个服务已经启动了。在服务运行期间,它会在指定的时间间隔内定期创建一个数据库项目记录。服务停止时会创建最后一条数据库记录。这个服务会自动向Windows应用程序日志当中登记下它成功启动或停止时的记录。
Visual Studio .NET能够使创建一个Windows服务变成相当简单的一件事情。启动我们的演示服务程序的说明概述如下。
1. 新建一个项目
2. 从一个可用的项目模板列表当中选择Windows服务
3. 设计器会以设计模式打开
4. 从工具箱的组件表当中拖动一个Timer对象到这个设计表面上 (注意: 要确保是从组件列表而不是从Windows窗体列表当中使用Timer)
5. 设置Timer属性,Enabled属性为False,Interval属性30000毫秒
6. 切换到代码视图页(按F7或在视图菜单当中选择代码),然后为这个服务填加功能
在你类后面所包含的代码里,你会注意到你所创建的Windows服务扩充了System.ServiceProcess.Service类。所有以.NET方式建立的Windows服务必须扩充这个类。它会要求你的服务重载下面的方法,Visual Studio默认时包括了这些方法。
• Dispose – 清除任何受控和不受控资源(managed and unmanaged resources)
• OnStart – 控制服务启动
• OnStop – 控制服务停止
Windows服务实例
这是一个测试案例,服务名为Service1 黑色部分为自动生成,红色部分为我加进去的代码,绿色为我加入的注释,此案例没有其他意义,只是将记录插入到数据库中。
1》:Service1.Designer.cs
namespace AutoSendMail
{
partial class Service1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 20000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// Service1
//
this.ServiceName = "Service1";
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
}
#endregion
private System.Timers.Timer timer1;
}
}
2》:Service1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace AutoSendMail
{
public partial class Service1 : ServiceBase
{
SqlConnection sqlconn = new SqlConnection(@"server=192.168.0.1/sql2005;database=HR512Main;uid=sa;pwd=123;connection timeout=60");
string sqlStr = string.Empty;
public Service1()
{
InitializeComponent();
}
/// <summary>
/// 设置timer1 的 Interval=10800000 (3个小时)
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
this.timer1.Enabled = true;
//exec();
}
protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
this.timer1.Enabled = false;
//exec();
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// 每隔3小时分别执行一次(距缴费日期还有3天、2天、1天和已到缴费日期时都发送一封邮件。)
exec(3);
exec(2);
exec(1);
exec(0);
}
#region 数据库连接操作
/// <summary>
/// 打开数据库连接
/// </summary>
private void openCnn()
{
if (sqlconn.State != ConnectionState.Open)
{
try
{
sqlconn.Open();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
private void closeCnn()
{
if (sqlconn != null)
{
if (sqlconn.State != ConnectionState.Closed)
{
sqlconn.Close();
}
}
}
/// <summary>
/// 连接数据库
/// </summary>
/// <returns>返回数据集</returns>
private DataSet sqlConnect()
{
openCnn();
SqlCommand sqlcomm = new SqlCommand(sqlStr, sqlconn);
sqlcomm.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
DataSet dssql = new DataSet();
try
{
sda.Fill(dssql);
}
catch (Exception ex)
{
dssql = null;
throw new Exception(ex.Message, ex);
}
finally
{
sda.Dispose();
closeCnn();
}
return dssql;
}
#endregion
#region 发送邮件代码
/// <summary>
/// 执行发送邮件
/// </summary>
/// <param name="dayCount">距缴费到期还剩余天数</param>
private void exec(int dayCount)
{
DataSet dsInfo = getEmailInfo(dayCount);//需要发送邮件的企业会员信息
if (dsInfo != null)
{
if (dsInfo.Tables[0].Rows.Count > 0)
{
DataSet ds2 = getTemplateDS(dayCount);
if (ds2 != null)
{
if (ds2.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dsInfo.Tables[0].Rows.Count; i++)
{
sendMailMethod(ds2.Tables[0].Rows[0]["FFromMail"].ToString(),
ds2.Tables[0].Rows[0]["FFromMailPwd"].ToString(),
ds2.Tables[0].Rows[0]["FDisName"].ToString(),
dsInfo.Tables[0].Rows[i]["FEmail"].ToString(),
ds2.Tables[0].Rows[0]["FSubject"].ToString(),
ds2.Tables[0].Rows[0]["FBody"].ToString());
}
}
}
}
}
}
/// <summary>
/// --搜索出需要缴费的企业会员信息,并发送邮件通知其缴费(提前三天)。
/// </summary>
/// <returns>返回需要发送邮件的数据集</returns>
private DataSet getEmailInfo(int dcount)
{
DataSet ds = new DataSet();
sqlStr = "select a.Fqyid,a.FQyName,a.FNumber,a.FEmail, b.FEndDate," +
"datediff(dd,getdate(),max(b.FEndDate)) as DtCount from Base_Member a left outer join " +
"(select FQYid,max(FEndDate) AS FEndDate from Base_Member_Charges where FMailIs=0 group by FQYID) b " +
" on a.Fqyid=b.Fqyid " +
" where a.FState=1 and datediff(dd,getdate(),(b.FEndDate))=" + dcount +
" group by a.Fqyid,a.FQyName,a.FNumber,a.FEmail,b.fenddate order by a.Fqyid ";
try
{
ds = sqlConnect();
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
return ds;
}
else
{
return null;
}
}
else
{
return null;
}
}
catch
{
return null;
}
//finally
//{
// if (ds != null)
// {
// ds.Dispose();
// }
//}
}
/// <summary>
/// 查询出要使用的邮件模板
/// </summary>
/// <returns></returns>
private DataSet getTemplateDS(int num)
{
DataSet dsTemp =new DataSet ();
sqlStr = "select Fid,FFromMail,FFromMailPwd,FDisName,FSubject,FBody,FStateStr " +
" from Base_MailTemplate where FStateStr='提醒用户缴费" + num + "'";
try
{
dsTemp = sqlConnect();
if (dsTemp != null)
{
if (dsTemp.Tables[0].Rows.Count == 1)
{
return dsTemp;
}
else
{
return null;
}
}
else
{
return null;
}
}
catch
{
return null;
}
//finally
//{
// if (dsTemp != null)
// {
// dsTemp.Dispose();
// }
//}
}
/// <summary>
/// 发送邮件核心代码
/// </summary>
/// <param name="oFFromMail">发送邮箱全地址</param>
/// <param name="oFFromMailPwd">发送邮箱</param>
/// <param name="oFDisName">与邮箱关联的关联显示名</param>
/// <param name="toMail">收件人邮箱全地址</param>
/// <param name="oFSubject">邮件标题</param>
/// <param name="oFBody">邮件内容</param>
/// <returns>返回是否发送成功</returns>
private void sendMailMethod(string oFFromMail, string oFFromMailPwd, string oFDisName, string toMail,
string oFSubject, string oFBody)
{
try
{
//以下设置服务器
MailMessage mailmsg = new MailMessage();
mailmsg.From = new MailAddress(oFFromMail, oFDisName);//"csboy163@163.com", "HR伴我行"
mailmsg.To.Add(toMail);//"bad.huan@163.com"
mailmsg.Subject = oFSubject;//"HR伴我行会员邮件"
mailmsg.Body = oFBody;//"您好啊,这是测试邮件!<br />此信由HR伴我行系统发出,请勿回复。"
mailmsg.BodyEncoding = System.Text.Encoding.UTF8;
mailmsg.IsBodyHtml = true;
SmtpClient sendmsg = new SmtpClient();
sendmsg.Host = "smtp.163.com";
sendmsg.UseDefaultCredentials = false;
sendmsg.Credentials = new NetworkCredential(oFFromMail, oFFromMailPwd);//"csboy163@163.com", "密码"
sendmsg.DeliveryMethod = SmtpDeliveryMethod.Network;
sendmsg.Send(mailmsg);
mailmsg.Dispose();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
#endregion
}
}
什么是Windows服务?
Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合。它没有用户界面,并且也不会产生任何可视输出。任何用户消息都会被写进Windows事件日志。计算机启动时,服务会自动开始运行。它们不要用户一定登录才运行,它们能在包括这个系统内的任何用户环境下运行。通过服务控制管理器,Windows服务是可控的,可以终止、暂停及当需要时启动。
Windows 服务,以前的NT服务,都是被作为Windows NT操作系统的一部分引进来的。它们在Windows 9x及Windows Me下没有。你需要使用NT级别的操作系统来运行Windows服务,诸如:Windows NT、Windows 2000 Professional或Windows 2000 Server。举例而言,以Windows服务形式的产品有:Microsoft Exchange、SQL Server,还有别的如设置计算机时钟的Windows Time服务。
创建一个Windows服务
我们即将创建的这个服务除了演示什么也不做。服务被启动时会把一个条目信息登记到一个数据库当中来指明这个服务已经启动了。在服务运行期间,它会在指定的时间间隔内定期创建一个数据库项目记录。服务停止时会创建最后一条数据库记录。这个服务会自动向Windows应用程序日志当中登记下它成功启动或停止时的记录。
Visual Studio .NET能够使创建一个Windows服务变成相当简单的一件事情。启动我们的演示服务程序的说明概述如下。
1. 新建一个项目
2. 从一个可用的项目模板列表当中选择Windows服务
3. 设计器会以设计模式打开
4. 从工具箱的组件表当中拖动一个Timer对象到这个设计表面上 (注意: 要确保是从组件列表而不是从Windows窗体列表当中使用Timer)
5. 设置Timer属性,Enabled属性为False,Interval属性30000毫秒
6. 切换到代码视图页(按F7或在视图菜单当中选择代码),然后为这个服务填加功能
在你类后面所包含的代码里,你会注意到你所创建的Windows服务扩充了System.ServiceProcess.Service类。所有以.NET方式建立的Windows服务必须扩充这个类。它会要求你的服务重载下面的方法,Visual Studio默认时包括了这些方法。
• Dispose – 清除任何受控和不受控资源(managed and unmanaged resources)
• OnStart – 控制服务启动
• OnStop – 控制服务停止
Windows服务实例
这是一个测试案例,服务名为Service1 黑色部分为自动生成,红色部分为我加进去的代码,绿色为我加入的注释,此案例没有其他意义,只是将记录插入到数据库中。
1》:Service1.Designer.cs
namespace AutoSendMail
{
partial class Service1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 20000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// Service1
//
this.ServiceName = "Service1";
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
}
#endregion
private System.Timers.Timer timer1;
}
}
2》:Service1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace AutoSendMail
{
public partial class Service1 : ServiceBase
{
SqlConnection sqlconn = new SqlConnection(@"server=192.168.0.1/sql2005;database=HR512Main;uid=sa;pwd=123;connection timeout=60");
string sqlStr = string.Empty;
public Service1()
{
InitializeComponent();
}
/// <summary>
/// 设置timer1 的 Interval=10800000 (3个小时)
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
this.timer1.Enabled = true;
//exec();
}
protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
this.timer1.Enabled = false;
//exec();
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// 每隔3小时分别执行一次(距缴费日期还有3天、2天、1天和已到缴费日期时都发送一封邮件。)
exec(3);
exec(2);
exec(1);
exec(0);
}
#region 数据库连接操作
/// <summary>
/// 打开数据库连接
/// </summary>
private void openCnn()
{
if (sqlconn.State != ConnectionState.Open)
{
try
{
sqlconn.Open();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
private void closeCnn()
{
if (sqlconn != null)
{
if (sqlconn.State != ConnectionState.Closed)
{
sqlconn.Close();
}
}
}
/// <summary>
/// 连接数据库
/// </summary>
/// <returns>返回数据集</returns>
private DataSet sqlConnect()
{
openCnn();
SqlCommand sqlcomm = new SqlCommand(sqlStr, sqlconn);
sqlcomm.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
DataSet dssql = new DataSet();
try
{
sda.Fill(dssql);
}
catch (Exception ex)
{
dssql = null;
throw new Exception(ex.Message, ex);
}
finally
{
sda.Dispose();
closeCnn();
}
return dssql;
}
#endregion
#region 发送邮件代码
/// <summary>
/// 执行发送邮件
/// </summary>
/// <param name="dayCount">距缴费到期还剩余天数</param>
private void exec(int dayCount)
{
DataSet dsInfo = getEmailInfo(dayCount);//需要发送邮件的企业会员信息
if (dsInfo != null)
{
if (dsInfo.Tables[0].Rows.Count > 0)
{
DataSet ds2 = getTemplateDS(dayCount);
if (ds2 != null)
{
if (ds2.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dsInfo.Tables[0].Rows.Count; i++)
{
sendMailMethod(ds2.Tables[0].Rows[0]["FFromMail"].ToString(),
ds2.Tables[0].Rows[0]["FFromMailPwd"].ToString(),
ds2.Tables[0].Rows[0]["FDisName"].ToString(),
dsInfo.Tables[0].Rows[i]["FEmail"].ToString(),
ds2.Tables[0].Rows[0]["FSubject"].ToString(),
ds2.Tables[0].Rows[0]["FBody"].ToString());
}
}
}
}
}
}
/// <summary>
/// --搜索出需要缴费的企业会员信息,并发送邮件通知其缴费(提前三天)。
/// </summary>
/// <returns>返回需要发送邮件的数据集</returns>
private DataSet getEmailInfo(int dcount)
{
DataSet ds = new DataSet();
sqlStr = "select a.Fqyid,a.FQyName,a.FNumber,a.FEmail, b.FEndDate," +
"datediff(dd,getdate(),max(b.FEndDate)) as DtCount from Base_Member a left outer join " +
"(select FQYid,max(FEndDate) AS FEndDate from Base_Member_Charges where FMailIs=0 group by FQYID) b " +
" on a.Fqyid=b.Fqyid " +
" where a.FState=1 and datediff(dd,getdate(),(b.FEndDate))=" + dcount +
" group by a.Fqyid,a.FQyName,a.FNumber,a.FEmail,b.fenddate order by a.Fqyid ";
try
{
ds = sqlConnect();
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
return ds;
}
else
{
return null;
}
}
else
{
return null;
}
}
catch
{
return null;
}
//finally
//{
// if (ds != null)
// {
// ds.Dispose();
// }
//}
}
/// <summary>
/// 查询出要使用的邮件模板
/// </summary>
/// <returns></returns>
private DataSet getTemplateDS(int num)
{
DataSet dsTemp =new DataSet ();
sqlStr = "select Fid,FFromMail,FFromMailPwd,FDisName,FSubject,FBody,FStateStr " +
" from Base_MailTemplate where FStateStr='提醒用户缴费" + num + "'";
try
{
dsTemp = sqlConnect();
if (dsTemp != null)
{
if (dsTemp.Tables[0].Rows.Count == 1)
{
return dsTemp;
}
else
{
return null;
}
}
else
{
return null;
}
}
catch
{
return null;
}
//finally
//{
// if (dsTemp != null)
// {
// dsTemp.Dispose();
// }
//}
}
/// <summary>
/// 发送邮件核心代码
/// </summary>
/// <param name="oFFromMail">发送邮箱全地址</param>
/// <param name="oFFromMailPwd">发送邮箱</param>
/// <param name="oFDisName">与邮箱关联的关联显示名</param>
/// <param name="toMail">收件人邮箱全地址</param>
/// <param name="oFSubject">邮件标题</param>
/// <param name="oFBody">邮件内容</param>
/// <returns>返回是否发送成功</returns>
private void sendMailMethod(string oFFromMail, string oFFromMailPwd, string oFDisName, string toMail,
string oFSubject, string oFBody)
{
try
{
//以下设置服务器
MailMessage mailmsg = new MailMessage();
mailmsg.From = new MailAddress(oFFromMail, oFDisName);//"csboy163@163.com", "HR伴我行"
mailmsg.To.Add(toMail);//"bad.huan@163.com"
mailmsg.Subject = oFSubject;//"HR伴我行会员邮件"
mailmsg.Body = oFBody;//"您好啊,这是测试邮件!<br />此信由HR伴我行系统发出,请勿回复。"
mailmsg.BodyEncoding = System.Text.Encoding.UTF8;
mailmsg.IsBodyHtml = true;
SmtpClient sendmsg = new SmtpClient();
sendmsg.Host = "smtp.163.com";
sendmsg.UseDefaultCredentials = false;
sendmsg.Credentials = new NetworkCredential(oFFromMail, oFFromMailPwd);//"csboy163@163.com", "密码"
sendmsg.DeliveryMethod = SmtpDeliveryMethod.Network;
sendmsg.Send(mailmsg);
mailmsg.Dispose();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
#endregion
}
}
| ||
|
相关文章推荐
- Windows服务程序,定时从数据库中拿出记录发送邮件
- 编写一个C#Windows服务程序,定时从数据库中拿出记录发送邮件。
- 编写一个Windows服务程序,定时从数据库中拿出记录发送邮件
- C#写Windows Service(windows服务程序)
- 利用PHP安装windows自动运行的服务,PHP程序可以实现长时间、自动运行、定时更新功能,直接可以用在项目中的类源代码
- cmd命令创建windows服务自动开启某个程序
- git 远程版本库,github提供服务原理,git自动更新发送邮件
- git 远程版本库,github提供服务原理,git自动更新发送邮件
- git 远程版本库,github提供服务原理,git自动更新发送邮件
- 开发Linux 服务程序与windows service程序的主要区别
- 使用Java Service Wrapper以windows服务的形式运行Java程序
- JavaBean邮件自动发送程序带附件
- 后台自动发送邮件的程序(java mail,web版)②
- C# 编写Windows Service(windows服务程序)
- Windows Azure Cloud Service (28) 在Windows Azure发送邮件(中)
- 使用Java Service Wrapper 把Java程序作为Windows系统服务
- 将node.js程序作为服务,并在windows下开机自动启动(使用forever)
- 使用job自动抓取网页数据发送邮件,以springboot服务启动
- 检测tomcat服务,实现崩溃自动重启并发送邮件提醒
- .net Windows服务程序和安装程序制作图解 及 VS 2010创建、安装、调试 windows服务(windows service)