Mail sending Function
First, let's talk about how to send an email in. Net .. Net has prepared classes related to sending emails for us. It is very convenient to call them directly. Below is a simple email notification class:
/// <Summary>
/// Mail Notification Service class.
/// </Summary>
Public class EmailNotificationService {
/// <Summary>
/// Construct an email notification service instance.
/// </Summary>
/// <Param> SMTP Server IP address </param>
/// <Param> whether to use SSL to connect to the SMTP server </param>
/// <Param> SMTP server port </param>
/// <Param> User name used to log on to the SMTP server </param>
/// <Param> logon password </param>
Public EmailNotificationService (
String smtpService,
Bool enableSSL,
Int port,
String loginName,
String password ){
This. m_smtpService = smtpService;
This. m_loginName = loginName;
This. m_password = password;
This. m_enableSSL = enableSSL;
This. m_port = port;
}
Private readonly string m_smtpService;
Private readonly string m_loginName;
Private readonly string m_password;
Private readonly bool m_enableSSL;
Private readonly int m_port;
/// <Summary>
/// Send an EMAIL notification to the specified EMAIL address.
/// </Summary>
/// <Param> name displayed in the "sender" column </param>
/// <Param> Target EMAIL address </param>
/// <Param> email title </param>
/// <Param> email content </param>
Public void SendTo (string senderName, string address, string title, string content ){
MailMessage mail = new MailMessage ();
Mail. To. Add (address );
Mail. From = new MailAddress (this. m_loginName, senderName, Encoding. UTF8 );
Mail. Subject = title;
Mail. Body = content;
Mail. BodyEncoding = Encoding. UTF8;
Mail. IsBodyHtml = false;
Mail. Priority = MailPriority. Normal;
SmtpClient smtp = new SmtpClient ();
Smtp. Credentials = new NetworkCredential (this. m_loginName, this. m_password );
Smtp. Host = this. m_smtpService;
Smtp. EnableSsl = this. m_enableSSL;
Smtp. Port = this. m_port;
Smtp. Send (mail );
}
}
In use, first construct an EmailNotificationService class, and then call the SendTo method. For example:
Emailicationicationservice mailNotificationService = new EmailNotificationService ("smtp.gmail.com", true, 587, "LoginName@gmail.com", "LoginPassword ");
MailNotificationService. SendTo ("SenderName", "TargetAddress@qq.com", "Title", "Content ");
Email sending Implementation Scheme
A class is created to send emails. The next question is when to call this class. Network communication is required for sending emails, which consumes a lot of time. In addition, the Send method of SmtpClient blocks the calling thread. Once this method is called, you must wait until the email is sent or an error occurs to end the method call. Therefore, you cannot put the call to EmailNotificationService in ASP.. NET page code. If this is done, the client will have to wait a long time to get a response, and the user experience is poor.
SmtpClient also has a SendAsync method. The difference between this method and the Send method is that SendAsync is asynchronous. After this method is called, a new thread is generated to Send emails, then the calling thread returns immediately without waiting for the mail to end. Can we use SendAsync instead of Send and call it in the page code? The answer is no. Although the client can quickly obtain the corresponding information, the email is not sent at all. This is caused by ASP. NET page lifecycle is determined by the characteristics of the client to the server, each request, the page will go through a process from generation to destruction, when the page is destroyed, the thread responsible for sending emails is forced to end before sending emails.
Due to the ASP. NET page lifecycle feature, we cannot place the calling code in the page code. We need a page-independent thread, which always exists when the website is running. My solution is to use a global object to manage a mail sending thread and maintain a linked list of emails to be sent. When a global object is created, there is no content in the linked list, and the mail sending thread is suspended. When an email is sent for processing on a page, the information related to the sent email is added to the linked list of the sent email. At this time, the linked list is not empty. The sending mail thread starts to work. The Mail Information in the linked list is retrieved and sent one by one until the linked list is empty, and the linked list is suspended again. This loop is repeated.
Mail sending Function
The basic idea has been fixed, and the next step is to write code for implementation. First, define a class to encapsulate the information about the mail to be sent. This article begins with an online contribution system as an example, so the information used here is related to this application.
/// <Summary>
/// Encapsulate the class of information required for sending an email.
/// </Summary>
Public class mailpolicyinfo {
/// <Summary>
/// Obtain or set the title of the manuscript.
/// </Summary>
Public string Title {
Get;
Set;
}
/// <Summary>
/// Obtain or set the author name of the manuscript.
/// </Summary>
Public string Author {
Get;
Set;
}
/// <Summary>
/// Obtain or set the author's email address.
/// </Summary>
Public string EmailAddress {
Get;
Set;
}
/// <Summary>
/// Obtain or set the manuscript status.
/// </Summary>
Public ArticleStatus {
Get;
Set;
}
}
Then there is the definition of the global object class. I use the single-piece mode to implement its global structure.
/// <Summary>
/// Class for mail sending processing.
/// </Summary>
Public class icationicationhandler {
/// <Summary>
/// Static instances of this type.
/// </Summary>
Private static readonly NotificationHandler g_instance = new NotificationHandler ();
/// <Summary>
/// Obtain the unique instance of this class.
/// </Summary>
Public static NotificationHandler Instance {
Get {
Return g_instance;
& Nbs