当前位置: 移动技术网 > IT编程>开发语言>c# > .NET实现定时发送邮件代码(两种方式)

.NET实现定时发送邮件代码(两种方式)

2019年07月18日  | 移动技术网IT编程  | 我要评论

有时候我们或许会遇到想在某一个时刻给别人发送一封邮件,就像是在生日的时候,但是我们又怕到时候忘记了,这时就应该

使用发送定时邮件的功能,但是这个定时发送邮件功能是怎么实现的呢?下面用两种方式实现.net定时发送邮件代码,具体请看下面内容。

实现思路、需求添加一个全局应用程序类global.asax

代码会在访问网站时运行

global.asax代码:

void application_start(object sender, eventargs e)
  {
   // 在应用程序启动时运行的代码
   system.timers.timer timer = new system.timers.timer();//设计时间间隔,如果一个小时执行一次就改为
   timer.elapsed += new system.timers.elapsedeventhandler(send);
   timer.autoreset = true;
   timer.enabled = true;
  }
 
  void application_end(object sender, eventargs e)
  {
   // 在应用程序关闭时运行的代码
   system.threading.thread.sleep();
   string strurl = "服务器地址";
   system.net.httpwebrequest _httpwebrequest = (system.net.httpwebrequest)system.net.webrequest.create(strurl);
   system.net.httpwebresponse _httpwebresponse = (system.net.httpwebresponse)_httpwebrequest.getresponse();
   system.io.stream _stream = _httpwebresponse.getresponsestream();//得到回写的字节流
   _httpwebresponse.close();
  }
 
  void application_error(object sender, eventargs e)
  {
   // 在出现未处理的错误时运行的代码
 
  }
 
  void session_start(object sender, eventargs e)
  {
   // 在新会话启动时运行的代码
 
  }
 
  void session_end(object sender, eventargs e)
  {
   // 在会话结束时运行的代码。 
   // 注意: 只有在 web.config 文件中的 sessionstate 模式设置为
   // inproc 时,才会引发 session_end 事件。如果会话模式设置为 stateserver
   // 或 sqlserver,则不引发该事件。
 
  }
 
  private void send(object sender, system.timers.elapsedeventargs e)
  {
   switch (datetime.now.hour)
   {
    case :
    case :
     sendemail();
     break;
    //default:
    // sendemail();
    // break;
   }
  }
  private void sendemail()
  {
   string mailfrom = system.configuration.configurationmanager.appsettings["mailfrom"].tostring();
   string mailuser = system.configuration.configurationmanager.appsettings["mailuser"].tostring();
   string mailpassword = system.configuration.configurationmanager.appsettings["mailpassword"].tostring();
   string hostip = system.configuration.configurationmanager.appsettings["mailhost"].tostring();
 
   list<string> mailaddress = new list<string>();
   string mailsubjct = "邮件主题"; 
   string mailbody = "邮件内容:";
   mailaddress.add("邮件地址");string strreturn = sendmail(mailsubjct, mailbody, mailfrom, mailaddress, hostip, mailuser, mailpassword, false);
  }

sendmail方法

 

 public static string sendmail(string mailsubjct, string mailbody, string mailfrom, list<string> mailaddress, string hostip, string username, string password, bool ssl)
   {
    string str = "";
    try
    {
     mailmessage message = new mailmessage
     {
      isbodyhtml = true,
      subject = mailsubjct,
      body = mailbody,
 
      from = new mailaddress(mailfrom)
     };
     for (int i = ; i < mailaddress.count; i++)
     {
      message.to.add(mailaddress[i]);
     }
     smtpclient client = new smtpclient
     {
      enablessl = ssl,
      usedefaultcredentials = false
     };
     networkcredential credential = new networkcredential(username, password);
     client.credentials = credential;
     client.deliverymethod = smtpdeliverymethod.network;
     client.host = hostip;
     client.port = x;
     client.send(message);
    }
    catch (exception exception)
    {
     str = exception.message;
    }
    return str;
   }

第二种方式:

定时发邮件可以用timer来设置时间,放在了global.asaxapplication_start里面

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(“**@**”, "密码"); //指定登录服务器的用户名和密码 
    sc.send(message);  //发送邮件 
  }


到此全部代码就写完了。

创建一个控制台程序,生成一个exe 采用windows的计划任务程序指定每天的某个时间点发送思路就是这个思路比服务简单

以上采用了两种方式分别实现了采用.net技术实现邮件定时发送功能,需要的朋友可以参考下。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网