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.相关代码如下:
this.timer1 = null;
if (!System.Diagnostics.EventLog.Exists("邮件发送"))
{
System.Diagnostics.EventLog.CreateEventSource("邮件发送服务", "邮件发送");
}
this.eventLog1.Log = "邮件发送";
this.eventLog1.Source = "邮件发送服务";
{
this.timer1.Enabled = false;
this.timer1.Stop();
this.timer1.Dispose();
eventLog1.WriteEntry("邮件发送服务停止");
}
{
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);
}
{
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)
{
}
}
{
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();
}
{
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