. NET scheduled Send mail

Source: Internet
Author: User
Tags mailmessage smtpclient

Add a global Application class global.asax

The code will run when you visit the site

Global.asax Code:

1     voidApplication_Start (Objectsender, EventArgs e)2     {3         //code to run when the application starts4System.Timers.Timer Timer =NewSystem.Timers.Timer (3600000);//design time interval, if one hours to execute once will be changed to 36000005Timer. Elapsed + =NewSystem.Timers.ElapsedEventHandler (Send);6Timer. AutoReset =true;7Timer. Enabled =true;8     }9 Ten     voidApplication_End (Objectsender, EventArgs e) One     { A         //code to run when the application shuts down -System.Threading.Thread.Sleep ( the); -         stringstrURL ="Server Address"; theSystem.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 byte stream - _httpwebresponse.close (); +     } -  +     voidApplication_Error (Objectsender, EventArgs e) A     { at         //code to run when an unhandled error occurs -  -     } -  -     voidSession_Start (Objectsender, EventArgs e) -     { in         //code to run when a new session is started -  to     } +  -     voidSession_End (Objectsender, EventArgs e) the     { *         //The code that runs at the end of the session.  $         //Note: Only the sessionstate mode in the Web. config file is set toPanax Notoginseng         //InProc, the Session_End event is not raised. If the session mode is set to StateServer -         //or SQL Server, the event is not raised.  the  +     } A  the     Private voidSend (Objectsender, System.Timers.ElapsedEventArgs e) +     { -         Switch(DateTime.Now.Hour) $         { $              Case 0: -              Case  -: - SendEMail (); the                  Break; -             //Default:Wuyi             //SendEMail (); the             //Break ; -         } Wu     } -     Private voidSendEMail () About     { $         stringMailfrom = system.configuration.configurationmanager.appsettings["Mailfrom"]. ToString (); -         stringMailuser = system.configuration.configurationmanager.appsettings["Mailuser"]. ToString (); -         stringMailPassword = system.configuration.configurationmanager.appsettings["MailPassword"]. ToString (); -         stringHostIP = system.configuration.configurationmanager.appsettings["MailHost"]. ToString (); A  +list<string> mailaddress =Newlist<string>(); the         stringMAILSUBJCT ="Message Subject";
stringMailbody ="Message content:";
Mailaddress.add ("e-mail address");stringStrreturn = SendMail (mailsubjct, Mailbody, Mailfrom, MailAddress, HostIP, Mailuser, MailPassword,false); -}
SendMail method
1  Public Static stringSendMail (stringMAILSUBJCT,stringMailbody,stringMailfrom, list<string> MailAddress,stringHostIP,stringUsernamestringPasswordBOOLSSL)2         {3             stringstr ="";4             Try5             {6MailMessage message =NewMailMessage7                 {8isbodyhtml =true,9Subject =Mailsubjct,TenBody =Mailbody, One  Afrom =Newmailaddress (Mailfrom) -                 }; -                  for(inti =0; i < Mailaddress.count; i++) the                 { - message. To.add (Mailaddress[i]); -                 } -SmtpClient client =NewSmtpClient +                 { -Enablessl =SSL, +useDefaultCredentials =false A                 }; atNetworkCredential credential =Newnetworkcredential (username, password); -Client. Credentials =credential; -Client. Deliverymethod =smtpdeliverymethod.network; -Client. Host =HostIP; -Client. Port =0x19; - client. Send (message); in             } -             Catch(Exception Exception) to             { +str =exception. Message; -             } the             returnstr; *}

. NET scheduled Send mail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.