When I wrote a notification service for the company, I found the above address online. I am very grateful to the original author for his creative work. The purpose of rewriting is to meet the requirements of running as a service:
1. To meet the requirements of multithreading, the mail sending service can run in the background, and the connection with the SMTP server is considered as an exclusive resource.
2. To meet the stability requirements, do not simply throw an exception to handle the error. After an exception occurs, wait for a certain period of time before trying again. If the exception persists after a period of time, it is considered as an SMTP error and the identity of the failed email sending is returned.
3. Simplified attributes and Methods. Mail-related information is not passed in as an attribute but as a send parameter. Only a non-overloaded send method is published. This class is used as the base class, and the interface method required by the notification service is written in another way.
The following code is rewritten:
Using System;
Using System. Text;
Using System. IO;
Using System. Net;
Using System. Net. Sockets;
Using System. Collections;
Using System. Threading;
Namespace Deep. SendEmail
{
# Region AspNetPager Server Control
/// <Summary>
/// Mail can be transmitted through the built-in SMTP mail service in Microsoft Windows 2000 or any SMTP Server
/// </Summary>
Public class SmtpMail
{
Private const string ENTER = "";
/// <Summary>
/// Set the language code. The default value is GB2312. You can set it to "" if not required ""
/// </Summary>
Private string m_charset = "GB2312 ";
/// <Summary>
/// Server interaction record
/// </Summary>
Private StringBuilder m_logs = new StringBuilder ();
Private string m_ErrCode;
/// <Summary>
/// SMTP error code hash table
/// </Summary>
Private Hashtable m_ErrCodeHT = new Hashtable ();
/// <Summary>
/// SMTP hash table with correct code
/// </Summary>