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

    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("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

  • 相关阅读:
    webform--常用的控件
    .net嵌入c#代码(投票练习)
    webform之session传值(临时数据的存储)与扩展属性 --(购物车练习)
    ASP.NET aspx页面中 写C#脚本; ASP.NET 指令(<%@%>);
    LinQ操作
    什么是C# Lambda表达式?形如:p=>p.abc
    winform基础
    3D计算机图形学读书笔记—Wat版本
    计算机图形学的领域与分类
    NetBeans中文乱码解决办法
  • 原文地址:https://www.cnblogs.com/yinpeng186/p/1848800.html
Copyright © 2020-2023  润新知