写了一个windows服务,每隔20秒向邮箱中发一封邮件
安装部署,启动服务之后邮件却发不出去,同样的代码在winform底下运行都是可以的
请帮忙看看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。 timer1.Interval = 20000;
timer1.Enabled = true; } protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
} private void timer1_Tick(object sender, EventArgs e)
{
//if (DateTime.Now.Hour == Convert.ToInt32(sendMailHour) &&
// DateTime.Now.Minute == Convert.ToInt32(sendMailMinute) &&
// DateTime.Now.Second == 0)
//{
sengmill_net();
//} }
private void sengmill_net()
{//.net smtp类进行邮件发送,支持认证,附件添加;
string from = "[email protected]"; //发件人邮箱
string to = "[email protected]"; //收件人邮箱
string body = "自动发送测试邮件";
string subject = "TestMail";
System.Net.Mail.SmtpClient client = new SmtpClient("mail.prosoft.com.cn");
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "12345");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(from, to, subject, body);
message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
message.IsBodyHtml = true;
client.Send(message);
}
}
}
安装部署,启动服务之后邮件却发不出去,同样的代码在winform底下运行都是可以的
请帮忙看看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。 timer1.Interval = 20000;
timer1.Enabled = true; } protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
} private void timer1_Tick(object sender, EventArgs e)
{
//if (DateTime.Now.Hour == Convert.ToInt32(sendMailHour) &&
// DateTime.Now.Minute == Convert.ToInt32(sendMailMinute) &&
// DateTime.Now.Second == 0)
//{
sengmill_net();
//} }
private void sengmill_net()
{//.net smtp类进行邮件发送,支持认证,附件添加;
string from = "[email protected]"; //发件人邮箱
string to = "[email protected]"; //收件人邮箱
string body = "自动发送测试邮件";
string subject = "TestMail";
System.Net.Mail.SmtpClient client = new SmtpClient("mail.prosoft.com.cn");
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "12345");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(from, to, subject, body);
message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
message.IsBodyHtml = true;
client.Send(message);
}
}
}
原因是Windows服务中的定时器必须用这个 System.Timers.Timer
而不应该用 System....Forms.里的到点激活的方法是 private void timer1_Elapse(object sender, EventArgs e) 不是 Tick !!!在它的Design.cs 代码里改掉类名,再改掉方法委托就好了