WindowsService实现邮件定时发送

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里不会有问题,但到了桌面应用程序就不太好使了。

将serviceProcessInstaller1->Accout属性,设为:LocalSystem(默认是User)。 
否则将会出现如下错误:

windows服务安装时,出错:System.ComponentModel.Win32Exception: 帐户名无效或不存在,或者密码对

于指定的帐户

 

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.相关代码如下:

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

 

 

 

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

 

 

 

ExpandedBlockStart.gif 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);
            }

 

 

 

ExpandedBlockStart.gif 编写系统日志,用于调试
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)
            {

            }
        }

 

 

ExpandedBlockStart.gif 时间间隔达到后执行的程序
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();
        }

 

 

 

ExpandedBlockStart.gif 邮件发送
  public   static   void  SendEmail()
        {
            MailMessage message 
=   new  MailMessage();
            message.From 
=   new  MailAddress( " sky186315@126.com " );
            message.To.Add(
"  676994187@qq.com " );
            message.Subject 
=   " 定时邮件测试 " + DateTime.Now;
            message.Body 
=   " TTTTTTTTT " ;
            SmtpClient smtp 
=   new  SmtpClient( " smtp.126.com " );
            smtp.DeliveryMethod 
=  SmtpDeliveryMethod.Network;
            smtp.Credentials 
=   new  NetworkCredential( " sky186315@126.com " " 123456 " );
            smtp.Send(message);
        }

 

8.流水帐OVER

转载于:https://www.cnblogs.com/yinpeng186/archive/2010/10/12/1848800.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值