Article From:https://www.cnblogs.com/wuyabaibsd/p/9687472.html
 protected override void OnStart(string[] args)
        {
            timer1 = new System.Timers.Timer();
            // timer1 = new System.Timers.Timer(24 * 60 * 60 * 1000);
            timer1.Interval = 3000;  //Set timer interval execution time
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
            timer1.Enabled = true;
            SendMail(GetType().Name, "Service start");
            File.AppendAllText("D:" + this.GetType().Name + ".txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss Service startup") + timer1.Enabled);
        }
 protected override void OnStop()
        {
            this.timer1.Enabled = false;
            SendMail(GetType().Name, "Service stop");
            File.AppendAllText("D:" + this.GetType().Name + ".txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss Service stop") + timer1.Enabled);
        }
  private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
//Timing service execution method

}

 

public static void SendMail(string title,string conn)
        {
            //Mailbox address sent
            var emailAcount = "";
            //The password for sending the mailbox
            var emailPassword = "";
            //To whom to send
            var to = "";
            //send content
            var content =conn;
            //Mail title
            var mailtitle =conn;
            //Mail server
            var mailservice ="";  //Below is mailbox server address.//Create mail sending instance
            MailMessage message = new MailMessage();
            //Setting the sender, the sender needs to be consistent with the mailbox sent by the sending mail server.
            MailAddress fromAddr = new MailAddress(emailAcount);
            message.From = fromAddr;
            //Set the addressee, add more than one, add the same method as below.
            message.To.Add(to);
            //Set mail headers
            message.Subject = mailtitle;
            //Setting mail content
            message.Body = content;
            //Set up the mail sending server, the server is different according to the mailbox you use, you can go to the corresponding mailbox management background to see, the following is QQ
            SmtpClient client = new SmtpClient(mailservice, 25);
            //Set the sender's mailbox number and password.
            client.Credentials = new NetworkCredential(emailAcount, emailPassword);
            //Enabling SSL, that is, sending safely.
            client.EnableSsl = true;
            //Send mail
            client.Send(message);
        }

Ali service mailbox: SMTP smtp.mxhichina.com port 25465

QQMailbox: smtp.qq.com port 587

Service installation method (administrator running CMD):

cd  LocationCD C:\Windows\Microsoft.NET\Framework\v4.0.30319   Locate the location of the InstallUtil file (most computers are here).Create input InstallUtil.exe+addressStart SC startStop SC stopDelete SC delete

 

Leave a Reply

Your email address will not be published. Required fields are marked *