Windows service 定时发送Mail
来源:互联网 发布:免费打电话软件 编辑:程序博客网 时间:2024/05/01 05:57
User提出了一个要求,即定时发Mail,一封Mail可能会预约到明天或是后天去发。
怎么办呢?
步骤如下:
1. 新建一个Windows服务项目
2. 设计器会以设计模式打开
3. 从工具箱的组件表当中拖动一个Timer对象到这个设计表面上
(注意:要实现Timer每隔1秒触发,必须引用System.Timer.Timer,此timer控件需从.net Framework组件中添加)
4. 设置Timer属性,Interval属性1000毫秒,Enable=true,AutoReset=true.
5.timer1_Tick事件代码:(若使用System.Timer.Timer,则为timer1_Elapsed)
6. 将这个服务程序切换到设计视图
7. 右击设计视图选择“添加安装程序”
8. 切换到刚被添加的ProjectInstaller的设计视图
9. 设置serviceInstaller1组件的属性:
1) ServiceName = myTest
2) StartType = Automatic (开机自动运行)
10. 设置serviceProcessInstaller1组件的属性 Account = LocalSystem
11. 改变路径到你项目所在的bin\Debug文件夹位置(如果你以Release模式编译则在bin\Release文件夹)
12. 执行如下批处理,请按实际情况加以修改:
install.bat:
怎么办呢?
步骤如下:
1. 新建一个Windows服务项目
2. 设计器会以设计模式打开
3. 从工具箱的组件表当中拖动一个Timer对象到这个设计表面上
(注意:要实现Timer每隔1秒触发,必须引用System.Timer.Timer,此timer控件需从.net Framework组件中添加)
4. 设置Timer属性,Interval属性1000毫秒,Enable=true,AutoReset=true.
5.timer1_Tick事件代码:(若使用System.Timer.Timer,则为timer1_Elapsed)
private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
string strSql = "select uniqueID,sendTime from bbs_mail where status='N'";
DataSet ds = db.GetDataSet(strSql);
if (ds.Tables[0].Rows.Count > 0)
{
//Time
string strSendTime = ds.Tables[0].Rows[0]["sendTime"].ToString();
//ID
string strID = ds.Tables[0].Rows[0]["uniqueID"].ToString();
//IF Datetime then Send Mail
if (DateTime.Parse(DateTime.Now.ToString("HH:mm:ss.f")) >= DateTime.Parse(DateTime.Parse(strSendTime).ToString("HH:mm:ss.f")))
{
sendMail(strID);
}
//else cycle
else
{
return;
}
}
}
void sendMail(string strID)
{
//query Data
string strSql = "select mailTo,subject,txt from bbs_mailAddr,bbs_mail where mailID='" + strID + "' and mailID=uniqueID";
DataSet ds = db.GetDataSet(strSql);
//Mail
MailMessage mailObj = new MailMessage();
//寄件人
mailObj.From = new MailAddress("*@*.com.cn");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
//收件人
mailObj.To.Add(ds.Tables[0].Rows[i]["mailTo"].ToString());
}
mailObj.Subject = ds.Tables[0].Rows[0]["subject"].ToString();
mailObj.Body = ds.Tables[0].Rows[0]["txt"].ToString();
mailObj.IsBodyHtml = true;
try
{
SmtpClient client = new SmtpClient("Exchange Server Name");
client.Send(mailObj);
//Update Status
strSql = "update bbs_mail set status='Y' where uniqueID='" + strID + "'";
db.ExecuteNonQuery(strSql);
//sb.Append("OK:"+DateTime.Now + ":" + strID + "发送成功!<br>");
}
catch (Exception pp)
{
// sb.Append("Error:"+ DateTime.Now+":" + pp.Message+"<br>");
}
// txtMsg.Text = sb.ToString();
}
{
string strSql = "select uniqueID,sendTime from bbs_mail where status='N'";
DataSet ds = db.GetDataSet(strSql);
if (ds.Tables[0].Rows.Count > 0)
{
//Time
string strSendTime = ds.Tables[0].Rows[0]["sendTime"].ToString();
//ID
string strID = ds.Tables[0].Rows[0]["uniqueID"].ToString();
//IF Datetime then Send Mail
if (DateTime.Parse(DateTime.Now.ToString("HH:mm:ss.f")) >= DateTime.Parse(DateTime.Parse(strSendTime).ToString("HH:mm:ss.f")))
{
sendMail(strID);
}
//else cycle
else
{
return;
}
}
}
void sendMail(string strID)
{
//query Data
string strSql = "select mailTo,subject,txt from bbs_mailAddr,bbs_mail where mailID='" + strID + "' and mailID=uniqueID";
DataSet ds = db.GetDataSet(strSql);
MailMessage mailObj = new MailMessage();
//寄件人
mailObj.From = new MailAddress("*@*.com.cn");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
//收件人
mailObj.To.Add(ds.Tables[0].Rows[i]["mailTo"].ToString());
}
mailObj.Subject = ds.Tables[0].Rows[0]["subject"].ToString();
mailObj.Body = ds.Tables[0].Rows[0]["txt"].ToString();
mailObj.IsBodyHtml = true;
try
{
SmtpClient client = new SmtpClient("Exchange Server Name");
client.Send(mailObj);
//Update Status
strSql = "update bbs_mail set status='Y' where uniqueID='" + strID + "'";
db.ExecuteNonQuery(strSql);
//sb.Append("OK:"+DateTime.Now + ":" + strID + "发送成功!<br>");
}
catch (Exception pp)
{
// sb.Append("Error:"+ DateTime.Now+":" + pp.Message+"<br>");
}
// txtMsg.Text = sb.ToString();
}
6. 将这个服务程序切换到设计视图
7. 右击设计视图选择“添加安装程序”
8. 切换到刚被添加的ProjectInstaller的设计视图
9. 设置serviceInstaller1组件的属性:
1) ServiceName = myTest
2) StartType = Automatic (开机自动运行)
10. 设置serviceProcessInstaller1组件的属性 Account = LocalSystem
11. 改变路径到你项目所在的bin\Debug文件夹位置(如果你以Release模式编译则在bin\Release文件夹)
12. 执行如下批处理,请按实际情况加以修改:
install.bat:
@echo off
echo **************************************************************
echo * Hi, *
echo * %username% :) 我是 Vegas Lee, 很高兴为您服务! *
echo * *
echo * ---------------------------------------- *
echo * *
echo **************************************************************
set Addr=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
cd/d %Addr%
installutil "C:\Documents and Settings\vegas lee\My Documents\Visual Studio 2005\Projects\PlatFormMailService\PlatFormMailService\bin\Debug\PlatFormMailService.exe"
sc start mytest
@pause
Uninstall.bat:echo **************************************************************
echo * Hi, *
echo * %username% :) 我是 Vegas Lee, 很高兴为您服务! *
echo * *
echo * ---------------------------------------- *
echo * *
echo **************************************************************
set Addr=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
cd/d %Addr%
installutil "C:\Documents and Settings\vegas lee\My Documents\Visual Studio 2005\Projects\PlatFormMailService\PlatFormMailService\bin\Debug\PlatFormMailService.exe"
sc start mytest
@pause
@echo off
echo **************************************************************
echo * Hi, *
echo * %username% :) 我是 Vegas Lee, 很高兴为您服务! *
echo * *
echo * ---------------------------------------- *
echo * 请按任意键进行卸载 *
echo * *
echo **************************************************************
sc stop mytest
sc delete mytest
pause
echo **************************************************************
echo * Hi, *
echo * %username% :) 我是 Vegas Lee, 很高兴为您服务! *
echo * *
echo * ---------------------------------------- *
echo * 请按任意键进行卸载 *
echo * *
echo **************************************************************
sc stop mytest
sc delete mytest
pause
- Windows service 定时发送Mail
- 【Demo】Windows Service 定时服务
- windows服务定时发送邮件
- windows 定时发送邮件服务
- 每天定时检查Oracle alert日志并发送mail
- CentOS 7.0 配置mail定时发送svn日志邮件
- 写一个Windows Service定时任务
- ASP.NET定时发送邮件&windows server
- windows下php客服端发送email mail()
- 使用flask mail发送mail
- Java发送邮件在Windows下使用mail发送邮件正常在linux下报错
- 定时发送
- Java mail Exchange Service
- 发送mail例子
- 自动发送E-mail
- php发送socket mail
- .NET发送Mail介绍
- java发送mail
- 我们如何让产品 backlog 停留在业务层次上
- 快捷支付单方便划款,支付宝绑架银行,网银安全关卡成马其诺防线?
- Hive和并行数据仓库的比较
- 点击超链接打开服务器上的word文件
- 黑马程序员:注解
- Windows service 定时发送Mail
- 《windows via C++》之windows线程同步
- Java 工具(jmap,jstack)在linux上的源码分析(四)safe point
- ADO.NET及SQL事務
- js调用后台方法与后台调用js方法
- E: Couldn't find package lib32z-dev
- 微软历史——.NET
- 黑马程序员:泛型
- 岁末