Asp.net timed email sending Summary

Source: Internet
Author: User

The Marketing Department of the Hong Kong company asked us to implement a regular email sending function, that is, to send the order information that has passed the third review to the marketing department manager at around five o'clock P.M. every day, tell him which orders have been finalized. At ordinary times, I only know how to use. Net to send emails, but I do not know how to regularly send emails. Baidu summarized the following three types:

(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.

The ERP system of the Hong Kong company is the BS model. Because the server environment conditions and Solutions of the other company need simple restrictions, my master and I decided to use the third method. Before programming, let's first introduce several methods in the global. asax file.

 

Protected   Void Application_start (Object sender, eventargs E) { // Application_start method: Request an ASP. NET applicationProgramIs called when the first resource (such as a page) in. 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 ); // The 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 a three-digit ticket is to be sent, no sender will be returned.
}
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, which is a unit of separation
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 will be called. {
String Subject =   String . Format ( " CO approve report ({0 }) " , Datetime. Now. tostring ( " Yyyy-mm-dd hh: mm: SS " ));
String Host = Configurationmanager. deleettings [ " Mailhost " ];
String From = Configurationmanager. deleettings [ " Mailfrom " ];
String To = Configurationmanager. deleettings [ " Mailto " ];
String User = Configurationmanager. deleettings [ " Mailuser " ];
String Password = Configurationmanager. deleettings [ " Mailpassword " ];
String Content = Getemailcontent. getmailcontent ();
Try
{
Ordermail. Send (host, user, password, to, from, subject, content, Null );// Change the email sending method to your own email sending method.
}
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 timer interval <= 2 * absolute and absolute are the gap values. For more information, see the definition of the yellow background above. The customer's requirements shall prevail, if this condition is not met, the mail sending method cannot be called within the specified period of time.

Good,CodeAfter the preparation is complete, 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). The problem is that no email is received on the day! 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. NET \ framework \ v2.0.50727 \ config \ machine. config, specific settings can refer to: http://www.zhiweinet.com/jiaocheng/2008-07/1145.htm

Also, many people may misunderstand the applicatioin_start method:Application_startIt is loaded when the first person accesses the website. It will only be called once and will not be called later. application_start is loaded when the first person accesses the website. This is correct, the premise is that during its lifecycle, the application pool will be restarted for the following reasons, 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 deleteSource codeFile.

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/1042746.html

Net garbage collection mechanism: http://blog.csdn.net/lerit/archive/2009/08/16/4451287.aspx

Appendix sqlserver 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. Because the communications system of the other company is internal access and the access volume is very small, there are no major performance requirements. The other party has not yet reflected performance problems, however, I think there must be other better but easy-to-implement solutions. Both QQ mail and 163 mail can send emails. Can you discuss how they are implemented.

 

 

 

 

 

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.