C# 需要窗口服务间隔的建议吗
我有窗口服务应用程序发送电子邮件每10秒,但我想在特定的日期发送它。我只想在那天发送一次/一封电子邮件。我需要将10秒改为1天吗?或者还有别的办法 代码:C# 需要窗口服务间隔的建议吗,c#,vb.net,email,send,C#,Vb.net,Email,Send,我有窗口服务应用程序发送电子邮件每10秒,但我想在特定的日期发送它。我只想在那天发送一次/一封电子邮件。我需要将10秒改为1天吗?或者还有别的办法 代码: 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统诊断; 使用System.Linq; 使用System.ServiceProcess; 使用系统文本; 使用System.Web; Net系统; 使用System.Net.Mail; 使用System.Net.Mime; 使用系
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统诊断;
使用System.Linq;
使用System.ServiceProcess;
使用系统文本;
使用System.Web;
Net系统;
使用System.Net.Mail;
使用System.Net.Mime;
使用系统线程;
使用MySql.Data;
使用MySql.Data.MySqlClient;
命名空间TestWS
{
公共部分类MyNewService:ServiceBase
{
来自_电子邮件的私人字符串=”dcaquino@trends.com.ph";
电子邮件的私人字符串=”dcaquino@trends.com.ph";
//私人字符串cc_email=”dcaquino@trends.com.ph";
System.Timers.Timer Timer=新的System.Timers.Timer();
公共MyNewService()
{
初始化组件();
this.CanStop=true;
this.CanPauseAndContinue=true;
}
启动时受保护的覆盖无效(字符串[]args)
{
timer.Stop();
timer.appead+=新系统.Timers.ElapsedEventHandler(sendmail);
timer.Interval=10000;//15分钟
timer.Enabled=true;
timer.AutoReset=true;
timer.Start();
//timer.Enabled=true;
//计时器。间隔=10000;
//timer.appeated+=新系统.Timers.ElapsedEventHandler(timer_appeated);
}
受保护的覆盖void OnStop()
{
}
//--------计时器----//
受保护的无效计时器(对象源,System.Timers.ElapsedEventArgs aa)
{
检查合同();
}
//---------发送电子邮件并检查合同结束日期-------//
私人无效检查合同()
{
string constring=“server=localhost;user=root;database=scms;port=3306;password=;”;
MySqlConnection conn=新的MySqlConnection(consting);
conn.Open();
string query=“选择a.contract\u end、a.contract\u id、b.title from contracts a join clients b on a.client\u id=b.id”;
MySqlCommand cmd1=新的MySqlCommand(查询,连接);
MySqlDataAdapter ad1=新的MySqlDataAdapter(cmd1);
DataTable dt1=新DataTable();
ad1.填充(dt1);
string querycount=“从合同中选择计数(合同结束)”;
MySqlCommand cmd2=新的MySqlCommand(querycount,conn);
MySqlDataAdapter ad2=新的MySqlDataAdapter(cmd2);
DataTable dt2=新的DataTable();
ad2.填充(dt2);
int count_dates=Convert.ToInt16(dt2.Rows[0][0].ToString());
对于(int i=0;i
”+
“合同将于“+到期日”到期+
“
剩余天数:”+剩余天数+
“
合同编号:”+c_id+
客户名称:“+客户名称”+
“
请准备必要的步骤更新客户以续订合同。
”+
“
来自SCMS的消息”;
smtpClient.Host=“10.10.20.20”;
smtpClient.Port=25;
smtpClient.Credentials=新系统.Net.NetworkCredential(“dcaquino@trends.com.ph“,”@dca12345”);
发送(消息);
smtpClient.ServicePoint.CloseConnectionGroup(smtpClient.ServicePoint.ConnectionName);
}
}
}
}
您可能需要的是cron作业类型的代码,我建议您在本文中使用
在那里你会发现一个我在Java中使用过的链接,它非常可靠。只需将上次发送邮件的日期和时间存储在注册表(或配置文件)中即可 在计时器检查中读取服务启动时的该值,查看您上次发送邮件的时间:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace TestWS
{
public partial class MyNewService : ServiceBase
{
private string from_email = "dcaquino@trends.com.ph";
private string to_email = "dcaquino@trends.com.ph";
//private string cc_email = "dcaquino@trends.com.ph";
System.Timers.Timer timer = new System.Timers.Timer();
public MyNewService()
{
InitializeComponent();
this.CanStop = true;
this.CanPauseAndContinue = true;
}
protected override void OnStart(string[] args)
{
timer.Stop();
timer.Elapsed += new System.Timers.ElapsedEventHandler(sendmail);
timer.Interval = 10000; // 15 min
timer.Enabled = true;
timer.AutoReset = true;
timer.Start();
//timer.Enabled = true;
//timer.Interval = 10000;
//timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
}
protected override void OnStop()
{
}
//--------TIMER----//
protected void timer_Elapsed(object source, System.Timers.ElapsedEventArgs aa)
{
check_contract();
}
//---------SEND EMAIL AND CHECK FOR THE CONTRACT END DATE-------//
private void check_contract()
{
string constring = "server=localhost;user=root;database=scms;port=3306;password=;";
MySqlConnection conn = new MySqlConnection(constring);
conn.Open();
string query = "select a.contract_end,a.contract_id,b.title from contracts a join clients b on a.client_id = b.id";
MySqlCommand cmd1 = new MySqlCommand(query, conn);
MySqlDataAdapter ad1 = new MySqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
ad1.Fill(dt1);
string querycount = "select count(contract_end) from contracts";
MySqlCommand cmd2 = new MySqlCommand(querycount, conn);
MySqlDataAdapter ad2 = new MySqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
ad2.Fill(dt2);
int count_dates = Convert.ToInt16(dt2.Rows[0][0].ToString());
for (int i = 0; i < count_dates; i++)
{
DateTime c_end = Convert.ToDateTime(dt1.Rows[i][0]);
DateTime c_end_30 = c_end.AddDays(-30);
DateTime c_end_15 = c_end.AddDays(-15);
DateTime c_end_10 = c_end.AddDays(-10);
string c_id = Convert.ToString(dt1.Rows[i][1]);
string client_name = Convert.ToString(dt1.Rows[i][2]);
string format = "MMMM dd, yyyy";
string date_expired = Convert.ToDateTime(dt1.Rows[i][0]).ToString(format);
if (c_end_30 == DateTime.Today)
{
sendemail(c_id, client_name, date_expired,"30 Days");
}
else if(c_end_15 == DateTime.Today)
{
sendemail(c_id, client_name, date_expired, "15 Days");
}
else if (c_end_10 == DateTime.Today)
{
sendemail(c_id, client_name, date_expired, "10 Days");
}
}
conn.Close();
}
private void sendemail(string c_id, string client_name, string date_expired,string days_remain)
{
SmtpClient smtpClient = new SmtpClient();
using (MailMessage message = new MailMessage())
{
MailAddress fromAddress = new MailAddress(from_email);
MailAddress toAddress = new MailAddress(to_email);
//MailAddress ccAddress = new MailAddress(ccAddress);
message.From = fromAddress;
message.To.Add(toAddress);
//message.CC.Add(ccAddress);
message.Subject = "Contract Expiration -SCMS";
message.IsBodyHtml = true;
message.Body = "Hello Account Manager, <br /> <br /> <br />" +
"Contract will expired on " + date_expired +
"<br /><br />Days Remaining: " + days_remain +
"<br /><br />Contract Number: " + c_id +
"<br /><br />Client Name: " + client_name +
"<br /><br /><br /><br />Please prepare for necessary steps to update the client for renewal of contract. <br/>" +
"<br /><br /><br />Message from SCMS";
smtpClient.Host = "10.10.20.20";
smtpClient.Port = 25;
smtpClient.Credentials = new System.Net.NetworkCredential("dcaquino@trends.com.ph", "@dca12345");
smtpClient.Send(message);
smtpClient.ServicePoint.CloseConnectionGroup(smtpClient.ServicePoint.ConnectionName);
}
}
}
}
'At Startup
Dim lastSend as DateTime = ReadFromRegistry
'In a Timer
If (DateTime.Now - lastSend).TotalDays >= 1 Then
'send mail
lastSend = DateTime.Now
'store lastSend in registry
End If