zoukankan      html  css  js  c++  java
  • 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;

            }

  • 相关阅读:
    http协议中的状态码(status code),超文本传输协议状态码
    web前端逻辑计算,血的教训
    js 关于字符串转数字及数字保留位数的控制
    js,setTimeout与setInterval的用法
    javaScript 字符串与unicode码之间的相互转换,函数的封装
    基于jquery,ajax请求及自我终止的函数封装。
    进入博客园后的第一篇文章
    答:SQLServer DBA 三十问之六:Job信息我们可以通过哪些表获取;系统正在运行的语句可以通过哪些视图获取;如何获取某个T-SQL语句的IO、Time等信息;
    答:SQLServer DBA 三十问之五:有关视图索引
    答:SQLServer DBA 三十问之三:有哪些操作会使用到TempDB;如果TempDB异常变大,可能的原因是什么,该如何处理
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175957.html
Copyright © 2011-2022 走看看