asp.net c# 定時發送郵件

來源:互聯網
上載者:User

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;

        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.