Windows service 定时发送Mail

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)

  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();

        }

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
/ % 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   off
echo 
**************************************************************
echo 
*  Hi,                                                         *
echo 
*    % username %  :) 我是 Vegas Lee, 很高兴为您服务!               *
echo 
*                                                              *
echo 
*             -- --------------------------------------         *
echo  *                      请按任意键进行卸载                   *
echo 
*                                                              *
echo 
**************************************************************
       
sc stop mytest
sc 
delete  mytest
pause









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值