文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>WindowsService实现邮件定时发送

WindowsService实现邮件定时发送

时间:2010-10-12  来源:FLY FREE

1.修改private System.Windows.Forms.Timer timer1;为private System.Timers.Timer timer1;

   修改this.timer1 = new System.Windows.Forms.Timer(this.components)为this.timer1 = new System.Timers.Timer()

2.系统默认的服务名称为service1,如何你想修改成自己的,可以修改serviceInstaller1的ServiceName

3.读取配置文件的方式,string mail=mail.MailFrom = ChinaNet5rService.Properties.Settings.Default["MailFrom"].ToString()//ChinaNet5rService是命名空间的名称,前提是你已经配置了App.config。一开始放在web.config里用System.Configuration.ConfigurationManager.AppSettings["MailFrom"].ToString() 读取出错,后来有位朋友说这种方式在B/S里不会有问题,但到了桌面应用程序就不太好使了。

4.可以写日志来调试错误,无法断点调试。

5.安装windowsservice,打开CMD,cd C:\Windows\Microsoft.NET\Framework\v2.0.50727;

InstallUtil.exe F:\公司项目\5RWeb\WebSite\Service\bin\Debug\Service.exe;

6.卸载windowsservice  

InstallUtil.exe /u F:\公司项目\5RWeb\WebSite\Service\bin\Debug\Service.exe;

7.相关代码如下:

Windows事件日志             InitializeComponent();
            this.timer1 = null;
            if (!System.Diagnostics.EventLog.Exists("邮件发送"))
            {
                System.Diagnostics.EventLog.CreateEventSource("邮件发送服务", "邮件发送");
            }
            this.eventLog1.Log = "邮件发送";
            this.eventLog1.Source = "邮件发送服务";

 

 

 

OnStop             if (this.timer1 != null)
            {
                this.timer1.Enabled = false;
                this.timer1.Stop();
                this.timer1.Dispose();
                eventLog1.WriteEntry("邮件发送服务停止");
            }

 

 

 

OnStart if (this.timer1 == null)
            {
                this.AddTextLine("Starting server……  ( " + DateTime.Now.ToString() + " )");
                eventLog1.WriteEntry("邮件发送启动");
                timer1 = new System.Timers.Timer();
                //每隔1分钟执行
                this.timer1.Interval = 0.5 * 60 * 1000;
                //eventLog1.WriteEntry("邮件服务刷新");
                //设置timer可以激发Elapsed事件
                this.timer1.Enabled = true;
                //开始
                this.timer1.Start();
                
                this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
            }

 

 

 

编写系统日志,用于调试 private void AddTextLine(string line)
        {
            try
            {
                FileStream fs = new FileStream(@"C:\Service1Log.txt", FileMode.OpenOrCreate, FileAccess.Write);

                StreamWriter m_streamWriter = new StreamWriter(fs);

                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

                m_streamWriter.WriteLine(line + "\r\n");

                m_streamWriter.Flush();

                m_streamWriter.Close();

                fs.Close();
            }
            catch (Exception ex)
            {

            }
        }

 

 

时间间隔达到后执行的程序 void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.timer1.Stop();
            DateTime now = DateTime.Now;
            this.AddTextLine("Judging time ……  ( " + DateTime.Now.ToString() + " )");
            string sendtime = ChinaNet5rService.Properties.Settings.Default["SendTime"].ToString();
            
            if (now.Hour.ToString() + ":" + now.Minute.ToString() == sendtime)
            {
                try
                {
                    this.AddTextLine("time is ok,Reading Sending ……  ( " + DateTime.Now.ToString() + " )");
                    send();
                }
                catch (Exception ex)
                {
                    this.AddTextLine("Sending error……  ( " + ex + " )");
                }
                eventLog1.WriteEntry("邮件发送成功");
                this.AddTextLine("Sending success");
            }
            this.timer1.Start();
            //throw new NotImplementedException();
        }

 

 

 

邮件发送  public static void SendEmail()
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress("[email protected]");
            message.To.Add(" [email protected]");
            message.Subject = "定时邮件测试"+DateTime.Now;
            message.Body = "TTTTTTTTT";
            SmtpClient smtp = new SmtpClient("smtp.126.com");
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential("[email protected]", "houjuan315");
            smtp.Send(message);
        }

 

8.流水帐OVER

相关阅读 更多 +
排行榜 更多 +
无限Fps

无限Fps

飞行射击 下载
幸存者时间僵尸

幸存者时间僵尸

飞行射击 下载
金属兄弟Metal Brother

金属兄弟Metal Brother

冒险解谜 下载