一个windows服务的简单问题

ralpha08 ralpha08 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
写了一个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 = "fengwei@prosoft.com.cn"; //发件人邮箱
string to = "fengwei@prosoft.com.cn"; //收件人邮箱
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("fengwei@prosoft.com.cn", "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);

}


}
}
...全文
评论 3
收藏 已收藏
分享
返回顶部
ralpha08
C# 111,128 成员 642,536 内容