Detailed description of Asp. Net timed mail sending Method

Source: Internet
Author: User

In summary, there are three types of emails sent using. net:
(1) make a winform to send regular mails. Then, schedule the task in windows and set it to the specified time. The task runs automatically each time. After the task is completed, the task is automatically disabled.
(2) Use sqlserver database to send mails, use sqlserver to store mails, and then create a job to run at a specified time.
(3) programming in the Global. asax file. Event: Application_Start. Use Time Programming. For example, the server executes a judgment once every second.

Before programming, let's first introduce several methods in the Global. asax file.
Protected void Application_Start (Object sender, EventArgs e)
{
// Application_start method: called when requesting the first resource (such as a page) in the ASP. NET application. Called only once during the lifecycle of an application
}
Protected void Application_End (Object sender, EventArgs e)
{
// Application_end method: Call the lifecycle of each application once before uninstalling the application.
}

The specific practices are as follows:
Code
Protected void Application_Start (Object sender, EventArgs e)
{
Timer t = new Timer (60000); // design interval. If one hour is executed, it is changed to 3600000. It is called once every minute.
T. Elapsed = new ElapsedEventHandler (t_Elapsed );
T. AutoReset = true;
T. Enabled = true;
}
Private void t_Elapsed (object sender, ElapsedEventArgs e)
{
If (GetEmailContent. GetMailContent (). Length = 0)
{
Return; // if the order has not passed the three-review, the system will return no email
}

Welcome to The. NET community forum and interact with 2 million technical staff> enter
Int sendTime_Hour = Convert. ToInt32 (ConfigurationManager. receivettings ["SendTime"]. ToString (); // if it is sent at pm
Int now_Hour = Convert. ToInt32 (DateTime. Now. Hour. ToString ());
Int now_Minute = Convert. ToInt32 (DateTime. Now. Minute. ToString ());
Int absolute = 1; // gap value, in minutes
If (now_Hour = sendTime_Hour-1) & (now_Minute> = 60-absolute) | (now_Hour = sendTime_Hour) & (now_Minute <= absolute) // if the time is between and, the following mail sending method is called.
{
String subject = string. Format ("CO Approve Report ({0})", DateTime. Now. ToString ("yyyy-MM-dd HH: mm: ss "));
String host = ConfigurationManager. receivettings ["MailHost"];
String from = ConfigurationManager. receivettings ["MailFrom"];
String to = ConfigurationManager. receivettings ["MailTo"];
String user = ConfigurationManager. receivettings ["MailUser"];
String password = ConfigurationManager. receivettings ["MailPassword"];
String content = GetEmailContent. GetMailContent ();
Try
{
OrderMail. Send (host, user, password, to, from, subject, content, null); // you can change the email sending method to your own one.
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}
}
}
If the time must be accurate to the minute, you can set the time range to seconds and the time interval of the timer to seconds. For example, if t_Elapsed is called once a second, the following conditions must be met: the time interval of the timer <2 * absolute, absolute is the gap value, see the definition of the yellow background above, the specific amount is subject to the customer's requirements, if this condition is not met, the mail sending method cannot be called within the specified period of time.

Okay, the Code has been compiled, and the test is OK. After work, set the email to be received at 09:01 PM (the theoretical value should be between PM and PM). No email was received that day. The problem is coming! Why didn't I receive an email at after work? I checked it online and found that I still had a problem. I didn't take it into consideration: the Application object has a lifecycle. When no one accesses the webpage or is idle for too long, the application pool calls the Application_End method to reclaim the object Resources in applicatioin, And the timer cannot work.

Solution: Set the collection time of the IIS application pool in IIS6.0 or later versions. The default value is 20 minutes. It can be set to be longer, but not too long, otherwise, the website may be suspended. Test again that night. You can officially send an email! It takes at least two or three months to send regular emails. IIS5.0 no application pool, can be set in C: WINDOWS Microsoft. NETFrameworkv2.0.50727CONFIG machine. config, specific settings can refer to: http://www.zhiweinet.com/jiaocheng/2008-07/1145.htm

In addition, many people may misunderstand the Applicatioin_Start method: Application_Start is loaded when the first person accesses the website. It will be called only once and will not be called in the future; application_Start is loaded when the first person accesses the website. This is correct, provided that the application pool is restarted for the following reasons during its lifecycle, that is, Application_Start can be called again:

1) Add, modify, or delete the assembly in the Bin folder of the application.
2) add, modify, or delete localized Resources in the App_GlobalResources or App_LocalResources folder.
3) add, modify, or delete the Global. asax file of the application.
4) add, modify, or delete source code files in the App_Code directory.
5) add, modify, or delete configuration files.
6) add, modify, or delete Web Service references in the App_WebReferences directory.
7) add, modify, or delete the Web. config file of the application.
Overview of application lifecycle: http://www.cnblogs.com/adsiz/archive/2008/01/17/4152746.html

NET garbage collection mechanism: http://blog.csdn.net/lerit/archive/2009/08/16/4451287.aspx
Appendix SQL Server mail solution: http://www.cnblogs.com/yjmyzz/archive/2008/09/04/1284229.html
Under normal circumstances, Application_Start is called only once, so that numerous timers are not instantiated and server resources are occupied, another problem is whether the timer time interval will occupy a lot of memory if it is accurate to seconds, therefore, you need to set the recycle time of the application pool and increase the interval set by the timer according to the specific situation.

The solution for sending mails at regular intervals is my personal practice for reference. It is not necessarily the best method. Both QQ mail and 163 mail can send emails. Can you discuss how they are implemented.

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.