WindowsService实现邮件定时发送

摘要:
将serviceProcessInstaller1-˃Accessout属性设置为LocalSystem。否则,将发生以下错误:安装Windows服务时,将发生错误:System.ComponentModel。Win32Exception:帐户名无效或不存在,或者密码是指定帐户的密码。4.可以写入日志以调试错误,并且无法执行断点调试。System.Diagnostics.EventLog.Exists){System.Diagnostics.EEventLog.CreateEventSource;}这eventLog1.Log=“发送邮件”;这eventLog1.Source=“邮件发送服务”;停止时(this.timer1!

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

WindowsService实现邮件定时发送第1张WindowsService实现邮件定时发送第2张Windows事件日志
            InitializeComponent();
            
this.timer1 = null;
            
if (!System.Diagnostics.EventLog.Exists("邮件发送"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
"邮件发送服务""邮件发送");
            }
            
this.eventLog1.Log = "邮件发送";
            
this.eventLog1.Source = "邮件发送服务";
WindowsService实现邮件定时发送第3张WindowsService实现邮件定时发送第4张OnStop
            if (this.timer1 != null)
            {
                
this.timer1.Enabled = false;
                
this.timer1.Stop();
                
this.timer1.Dispose();
                eventLog1.WriteEntry(
"邮件发送服务停止");
            }

 

WindowsService实现邮件定时发送第5张WindowsService实现邮件定时发送第6张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);
            }
WindowsService实现邮件定时发送第7张WindowsService实现邮件定时发送第8张编写系统日志,用于调试
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)
            {

            }
        }
WindowsService实现邮件定时发送第9张WindowsService实现邮件定时发送第10张时间间隔达到后执行的程序
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();
        }
WindowsService实现邮件定时发送第11张WindowsService实现邮件定时发送第12张邮件发送
 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

免责声明:文章转载自《WindowsService实现邮件定时发送》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇使用Libreoffice处理office文档,OFFICE转PDF,OFFICE转图片,OFFICE转图片包含加水印逻辑smokeping Master/Slave安装配置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF 导出EXCEL 方法

是用WPF将数据导出成EXCEL其实和其他.NET应用是相通的,ASP.NET也好WINFORM也好,都是用相同的方法实现,唯一不同的是ASP.NET中可能会存在用户权限的问题,毕竟ASP.NET的执行用户是IIS指定的用户而不是默认的系统用户。 具体实现方法如下,代码中使用完整的名称空间,便于理解 第一步,不许引用Excel的程序集,不同于网上其他文章,...

注册表比较工具

RegShot 是一种注册表比较工具,它通过两次抓取注册表而快速地比较出答案。它还可以将您的注册表以纯文本方式记录下来,便于浏览;还可以监察 Win.ini,System.ini 中的键值;还可以监察您 Windows 目录和 System 目录中文件的变化,为您手工卸载某些软件创造条件。 1.下载地址 https://sourceforge.net/pr...

驱动下通过进程PID获得进程名 (动态获取ImageFileName在EPROCESS结构体中的相对偏移)

思路 进程EPROCESS结构体中含有进程名ImageFileName(需求处ImageFileName在EPROCESS结构体中的相对偏移)——》获得进程EPROCESS——》通过进程句柄获得EPROCESS——》通过进程PID打开进程获得进程句柄 计算ImageFileName在EPROCESS结构体中的相对偏移 方法一 来个判断操作系统,再利用win...

转载->C#异常处理

C#异常处理 异常是在程序执行期间出现的问题。C# 中的异常是对程序运行时出现的特殊情况的一种响应,比如尝试除以零。 异常提供了一种把程序控制权从某个部分转移到另一个部分的方式。C# 异常处理时建立在四个关键词之上的:try、catch、finally和throw。 try:一个 try 块标识了一个将被激活的特定的异常的代码块。后跟一个或多个 cat...

Control.CheckForIllegalCrossThreadCalls=false不可在多线中随便使用

CodeProject上有篇文章An Alternate Way of Writing a Multithreaded GUI in C# 本意是Alternate Way 另外一种方法,后来莫名其妙的被转载成中文变了题目,最高效的方法。         CheckForIllegalCrossThreadCalls和control.Invoke有什么不同...

service之class core和class main

class core 和class main有何不同 在分析Kitkat加密功能的时候,在一些资料上看到一句话“对于加密后的系统,重启后,进入到输入密码界面,此时并没有完全加载Android系统,仅仅加载必要的服务” 那么这个必要的服务又是指的是那些服务呢? 在解答这个问题前我们先区分一下class core ,class main, class late...