C# 检查SSL证书到期日期并在指定的计划时间自动发送邮件的Windows服务
我是新手,我正在编写一个代码,该代码将检查SSL证书的到期日期,并定期或按计划时间向受尊敬的用户发送邮件。在配置设置中,我将间隔设置为一分钟,并将代码设置为在名为SSLLog.txt的文本文件中写入某些事件。我能够以本地用户的身份运行它,并且它作为服务成功运行,但它不会向受尊敬的用户发送邮件。请帮忙。下面是Service1.cs的代码C# 检查SSL证书到期日期并在指定的计划时间自动发送邮件的Windows服务,c#,asp.net,windows-services,C#,Asp.net,Windows Services,我是新手,我正在编写一个代码,该代码将检查SSL证书的到期日期,并定期或按计划时间向受尊敬的用户发送邮件。在配置设置中,我将间隔设置为一分钟,并将代码设置为在名为SSLLog.txt的文本文件中写入某些事件。我能够以本地用户的身份运行它,并且它作为服务成功运行,但它不会向受尊敬的用户发送邮件。请帮忙。下面是Service1.cs的代码 using System; using System.Collections.Generic; using System.ComponentModel; usin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.IO;
using System.Threading;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Security;
namespace WindowsServiceCS
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.WriteToFile("Simple Service started {0}");
this.ScheduleService();
}
protected override void OnStop()
{
this.WriteToFile("Simple Service stopped {0}");
this.Schedular.Dispose();
}
private Timer Schedular;
public void ScheduleService()
{
try
{
Schedular = new Timer(new TimerCallback(SchedularCallback));
string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();
this.WriteToFile("Simple Service Mode: " + mode + " {0}");
//Set the Default Time.
DateTime scheduledTime = DateTime.MinValue;
if (mode == "DAILY")
{
//Get the Scheduled Time from AppSettings.
scheduledTime = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings["ScheduledTime"]);
if (DateTime.Now > scheduledTime)
{
//If Scheduled Time is passed set Schedule for the next day.
scheduledTime = scheduledTime.AddDays(1);
}
}
if (mode.ToUpper() == "INTERVAL")
{
//Get the Interval in Minutes from AppSettings.
int intervalMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["IntervalMinutes"]);
//Set the Scheduled Time by adding the Interval to Current Time.
scheduledTime = DateTime.Now.AddMinutes(intervalMinutes);
if (DateTime.Now > scheduledTime)
{
//If Scheduled Time is passed set Schedule for the next Interval.
scheduledTime = scheduledTime.AddMinutes(intervalMinutes);
}
}
TimeSpan timeSpan = scheduledTime.Subtract(DateTime.Now);
string schedule = string.Format("{0} day(s) {1} hour(s) {2} minute(s) {3} seconds(s)", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
this.WriteToFile("Simple Service scheduled to run after: " + schedule + " {0}");
//Get the difference in Minutes between the Scheduled and Current Time.
int dueTime = Convert.ToInt32(timeSpan.TotalMilliseconds);
//Change the Timer's Due Time.
Schedular.Change(dueTime, Timeout.Infinite);
}
catch (Exception ex)
{
WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);
//Stop the Windows Service.
using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
{
serviceController.Stop();
}
}
}
private void SchedularCallback(object e)
{
try
{
ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback;
var request = WebRequest.Create("https://www.google.co.in");
var response = request.GetResponse();
WriteToFile("Check Completed");
}
catch(Exception ex)
{
WriteToFile("The Exception caused is " + ex);
}
this.WriteToFile("Simple Service Log: {0}");
this.ScheduleService();
}
private bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
try
{
WriteToFile("Certificate expires on " + certificate.GetExpirationDateString());
string name = "Tillapudi Venkata Sundeep";
string email = "xxxxxxxx1@gmail.com";
WriteToFile("Trying to send email to: " + name + " " + email);
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("xxxxxxxx@gmail.com", "xxxxxxxxx");
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage("xxxxxxxx@gmail.com", "xxxxxxxx1@gmail.com", "Test Mail Sent From Visual Studio", "Hey buddy How are you doing");
mm.BodyEncoding = System.Text.UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnFailure;
WriteToFile("Mail Sent");
client.Send(mm);
WriteToFile("Mail Sent");
}
catch(Exception exce)
{
WriteToFile("The Execption caused in the mail" +exce);
}
return true;
}
private void WriteToFile(string text)
{
string path = "C:\\SSlLog.txt";
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(string.Format(text, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
writer.Close();
}
}
}
}