asp.net c# 定时发送邮件

C# code

protected override void OnStart(string[] args)
        {
            MyTimer();
        }

        //实例化System.Timers.Timer  
        private void MyTimer()
        {
            //设置时间间隔
            System.Timers.Timer MT = new System.Timers.Timer(int.Parse(ConfigResource.Interval)*60*1000);
            MT.Elapsed += new System.Timers.ElapsedEventHandler(MTimedEvent);
            MT.Enabled = true;

        }
        //构造System.Timers.Timer实例   间隔时间事件 (定时执行事件)
        private void MTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
            //开始工作
            StartWork();
        }

        public void StartWork()
        {
             //从数据库DB查询表A中的时间   代码省略。。。
               //时间比较
               if(...)   //时间大于当前系统时间
             {
                  //发送邮件
                    int iStatus = SendMail("你指定的收件人Email地址","标题","内容");
                  if( iStatus > 0)
                  {
                       using (StreamWriter sw = new StreamWriter(filePath + "log.txt", System.Text.Encoding.GetEncoding("utf-8")))
                         {
                             sw.Wirte(System.DateTime.Now.ToString() + " 发送邮件成功!")
                         }
                  }
                  else{//失败}
             }
        }

/// <summary>
        /// 发送EMAIL
        /// </summary>
        /// <param name="sRecipientEmail">收件人地址</param>
        /// <param name="sSubject">主题</param>
        /// <param name="sMessage">内容</param>
        /// <returns>发送是否成功</returns>
        public bool SendMail(string sRecipientEmail, string sSubject, string sMessage)
        {

            //邮件对象
            MailMessage emailMessage;

            //smtp客户端对象

            SmtpClient client;

            // 初始化邮件对象

            String sSenderEmail = "你的邮箱";

emailMessage = new MailMessage(sSenderEmail, sRecipientEmail, sSubject, sMessage);
            emailMessage.IsBodyHtml = true;
            emailMessage.SubjectEncoding = System.Text.Encoding.Default;
            emailMessage.BodyEncoding = System.Text.Encoding.Default;
            //加入
            emailMessage.Headers.Add("X-Priority", "3");
            emailMessage.Headers.Add("X-MSMail-Priority", "Normal");
            emailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
            emailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");

            //邮件发送客户端
            client = new SmtpClient();

            //邮件服务器及帐户信息
            client.Host = "邮件服务器";
            //client.Host = "smtp.163.com";
           
            //client.Port = 465;
            //client.EnableSsl = true;
            System.Net.NetworkCredential Credential = new System.Net.NetworkCredential();
         
            Credential.UserName = "你的邮箱帐号"   //可以在资源文件中配置
            Credential.Password = "密码"

            client.Credentials = Credential;

            try
            {
                client.Send(emailMessage);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;

        }

posted on 2008-10-07 16:19  风乔  阅读(347)  评论(0编辑  收藏  举报

导航