[Asp.net] Web 定时自动发邮件

1. 添加Global.asax文件

add-->web--> Global Application Class



2. 在Global.asax里添加代码


protected void Application_Start(object sender, EventArgs e)
        {
            System.Timers.Timer timer = new System.Timers.Timer(60000);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(Send);
            timer.Start(); 
        }

        public void Send(object sender, System.Timers.ElapsedEventArgs e)
        {
            
            if (DateTime.Now.Minute == 20)
            {
                SendMail sm = new SendMail();
                sm.SendEMail("[email protected]", "[email protected]", "[email protected]", "Auto Mail", "This is a auto amil!");

            }
            
            
        }
        public void SendEMail(string To1, string CC1, string BC1, string Subject1, string Body1)
        {
            MailMessage msg = new MailMessage("[email protected]", To1);
            msg.CC.Add(CC1);
            msg.Bcc.Add(BC1);
            msg.Subject = Subject1;
            msg.Body = Body1;
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.High;
            SmtpClient c = new SmtpClient("127.0.0.1");
            c.Port = 25;
            c.Send(msg);
        }


猜你喜欢

转载自blog.csdn.net/shylx123/article/details/11737783