Often e-mail friends know that the mailbox has a special function, you can set http://www.php.cn/code/6259.html "target=" _blank "> E-mail delivery time, timed to send, this function is how to achieve it?" Next, the small series to share. NET implementation of the regular sending of mail code, the need for friends can refer to the following
Sometimes we may come across a time when we want to send an email to someone, like on a birthday, but we are afraid that we will forget it, then we should
Using the ability to send timed messages, but how is this timed Send mail feature implemented? Here are two ways to implement. NET scheduled send mail code, please see below for details.
Implement ideas and requirements add a global application class global.asax
The code will run when you visit the site
Global.asax Code:
void Application_Start (object sender, EventArgs e) {//code to run at application startup System.Timers.Timer Timer = new SYSTEM.TIMERS.T Imer ();//design interval, if one hours to execute the timer is changed. Elapsed + = new System.Timers.ElapsedEventHandler (Send); Timer. AutoReset = true; Timer. Enabled = true; } void Application_End (object sender, EventArgs e) {//code that runs when the application shuts down System.Threading.Thread.Sleep (); String strURL = "server address"; 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 ();//Get write-back of the byte stream _httpwebresponse.close (); } void Application_Error (object sender, EventArgs e) {//code to run in case of unhandled error} void Session_Start (object sender, Eve Ntargs e) {//code run at new session startup} void Session_End (object sender, EventArgs e) {//code that runs at the end of the session. Note: Only the sessionstate mode setting in the Web. config fileThe Session_End event is only raised for//InProc. If the session mode is set to StateServer//or SQL Server, the event is not raised. } private void Send (object sender, System.Timers.ElapsedEventArgs e) {switch (DateTime.Now.Hour) {CASE:CA Se: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 = "message subject"; String mailbody = "message content:"; Mailaddress.add ("Mail Address"); string strreturn = SendMail (mailsubjct, Mailbody, Mailfrom, Mailaddre SS, HostIP, Mailuser, MailPassword, false); }
SendMail method
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; }
The second way:
Timed email can be set by a timer , placed in the Global.asax Application_Start inside
using System.Net.Mail; using System.Timers; protected void Application_Start (object sender , EventArgs e) {Timer t = new timer (60000);//design time interval, if one hours is executed once, change to 3600000, here one minute call t.elapsed + = new Elapsedevent Handler (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); The recipient mailbox address can be multiple to achieve a bulk message. Subject = MessageSubject; Message. Body = MessageBody; Message. Isbodyhtml = true; is a message in HTML format. priority = Mailpriority.high; Priority level for sending messages SmtpClient sc = new SmtpClient (); Sc. Host = "smtp.sina.com"; Specifies the server address or IP sc for sending mail. Port = 25; Specifies the Send mail port//sc. useDefaultCredentials = true; Sc. Enablessl = true; Sc. Credentials = new System.Net.NetworkCredential ("**@**", "password"); Specify the user name and password for the login server SC. Send (message); Send Message}
All this code will be finished.
Create a console program that generates an EXE using Windows Scheduled task program to specify a point in time every day to send the idea is that this idea is simpler than the service