您的位置:首页 > 大数据 > 人工智能

【Vegas原创】Windows service使用初探:定时发送Mail

2014-05-22 15:45 344 查看
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/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 off


echo **************************************************************


echo * Hi,                                                        *


echo *  %username% :) 我是 Vegas Lee, 很高兴为您服务!              *


echo *                                                            *


echo *           ----------------------------------------         *


echo *                     请按任意键进行卸载

                  *


echo *                                                            *


echo **************************************************************


       


sc stop mytest


sc delete mytest


pause















内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: