vs C# 定时发邮件

定时发邮件可以用Timer来设置时间,我放在了Global.asax的Application_Start里面,关于Global.asax不明白的可以单独查一下
using System.Net.Mail;
using System.Timers;

protected void Application_Start(object sender, EventArgs e)
        {
            Timer t = new Timer(60000);//设计时间间隔,如果一个小时执行一次就改为3600000 ,这里一分钟调用一次
            t.Elapsed += new ElapsedEventHandler(t_Elapsed);
            t.AutoReset = true;
            t.Enabled = true;

        }

        private void t_Elapsed(object sender, ElapsedEventArgs e)
        {
            MailMessage message = new MailMessage();
            message.From = Messagefrom;
            message.To.Add(MessageTo);              //收件人邮箱地址可以是多个以实现群发
            message.Subject = MessageSubject;
            message.Body = MessageBody;
            message.IsBodyHtml = true;              //是否为html格式
            message.Priority = MailPriority.High;  //发送邮件的优先等级
            SmtpClient sc = new SmtpClient();
            sc.Host = "smtp.sina.com";              //指定发送邮件的服务器地址或IP
            sc.Port = 25;                          //指定发送邮件端口
            //sc.UseDefaultCredentials = true;
            //sc.EnableSsl = true;
            sc.Credentials = new System.Net.NetworkCredential(“[email protected]”, "密码"); //指定登录服务器的用户名和密码
                sc.Send(message);      //发送邮件

        }

你可能感兴趣的:(html,C++,c,.net,C#)