C# general email sending class sharing
This type of functions includes sending emails, whether the mailbox format is correct, and whether the mailbox username and password are correct without sending emails. Given the delay in the return result when POP checks the error of the mailbox username and password, use asynchronous thread to solve this problem, see the code:
using System; using ; using ; using ; using ; using ; using ; using ; using ; using ; using ; using ; namespace { /// <summary> /// Email /// </summary> public class CheckEmailInfo { public string server { get; set; }//server public string user { get; set; }//username public string pwd { get; set; }//password } /// <summary> /// SendEmail generic class, send emails through smtp server /// </summary> public class SendEmail { public Dictionary<string, string> smtpServer; public Dictionary<string, string> popServer; public SendEmail() { IniSmtpServer(); IniPopServer(); } /// <summary> /// Initialize the commonly used smtpServer to bind the drop-down selection menu /// </summary> private void IniSmtpServer() { smtpServer = new Dictionary<string, string>(); ("NetEase 163 Email", "smtp."); ("NetEase vip.163 Email", "."); ("NetEase 126 Email", "smtp."); ("NetEase 188 Email", "smtp."); ("Sina Email", ""); ("Yahoo Mail", ""); ("Sohu Email", ""); ("TOM Email", ""); ("Gmail Email", ""); ("QQ Email", ""); ("QQ Enterprise Email", ""); ("139 Email", "smtp."); ("263 Email", "smtp."); } /// <summary> /// Initialize the commonly used popServer, used to bind the drop-down selection menu /// </summary> private void IniPopServer() { popServer = new Dictionary<string, string>(); ("NetEase 163 Email", "pop3."); ("NetEase vip.163 Email", "."); ("NetEase 126 Email", "pop3."); ("NetEase 188 Email", "pop3."); ("Sina Email", ""); ("Yahoo Mail", ""); ("Sohu Email", ""); ("TOM Email", ""); ("Gmail Email", ""); ("QQ Email", ""); ("QQ Enterprise Email", ""); ("139 Email", "pop."); ("263 Email", "pop."); } /// <summary> /// Send email function /// </summary> /// <param name="fromEmail">Login email</param> /// <param name="password">Login password</param> /// <param name="user">Email nickname</param> /// <param name="title">Email title</param> /// <param name="toEmail">Email Address</param> /// <param name="email">Email content</param> /// <param name="smtpServer">smtp Server</param> public bool SendMessage(string fromEmail,string password, string user, string title, string toEmail, string email,string smtpServer) { try { SmtpClient smtp = new SmtpClient(); //Instantiate a SmtpClient = ; //Set the outbound method of smtp to Network = false;// Whether SSL encryption is enabled on the smtp server = smtpServer;//Specify smtp server = new NetworkCredential(fromEmail, password); MailMessage mm = new MailMessage(); //Instantiate an email class = ; //The priority of emails is divided into Low, Normal, High, usually Normal is used = new MailAddress(fromEmail, user, (936)); (new MailAddress(toEmail, "", (936))); = title; //Email Title = (936); = true; //Is the email body in HTML format = (936); = email; (mm); return true; } catch { return false; } } /// <summary> /// Check whether the email address is correct /// </summary> delegate bool MyDelegate(object checkEmailInfo); /// <summary> /// Use asynchronous method to check whether the email account and password are correct /// </summary> public bool CheckUser(string server, string user, string pwd) { MyDelegate myDelegate = new MyDelegate(CheckUser); CheckEmailInfo checkEmailInfo = new CheckEmailInfo(); = server; = user; = pwd; IAsyncResult result = (checkEmailInfo, null, null); (1000);//The main thread checks whether the asynchronous thread has been completed after 1 second if () { return (result); }//If the wrong mailbox and password are incorrect, the function will run very slowly else { return false; } } /// <summary> /// Determine whether the user's email account and password are correct /// </summary> /// <param name="server">PopServer address</param> /// <param name="user">username</param> /// <param name="pwd">Password</param> private bool CheckUser(object checkEmailInfo) { CheckEmailInfo checkInfo = (CheckEmailInfo)checkEmailInfo; TcpClient sender = new TcpClient(, 110);//The pop protocol uses TCP's 110 port Byte[] outbytes; NetworkStream ns; StreamReader sr; string input; string readuser = ; string readpwd = ; try { ns = (); sr = new StreamReader(ns); (); //Check username and password input = "user " + + "\r\n"; outbytes = (()); (outbytes, 0, ); readuser = (); input = "pass " + + "\r\n"; outbytes =(()); (outbytes, 0, ); readpwd = (); if ((0, 3) == "+OK" && (0, 3) == "+OK") { return true; } else { return false; } } catch { return false; } } /// <summary> /// Determine whether the email format is correct /// </summary> /// <param name="email">Email address</param> public bool IsEmail(string email) { string paterner = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"; if (!(email, paterner)) { return false;} else {return true;} } } }
Related Articles
C# BackgroundWorker usage method
This article mainly introduces the usage method of c# BackgroundWorker. The code in the article is very detailed to help everyone better refer to and learn. Interested friends can learn more2020-06-06Example code for C# implementing single-threaded asynchronous mutex locks
The function of asynchronous mutex is to ensure that there is a context synchronous mutex for asynchronous operations. This article mainly introduces in detail how C# can implement a single-threaded asynchronous mutex. The example code in the article is explained in detail. If you need it, please refer to it.2024-01-01C# implements student management system
This article mainly introduces the C# student management system to you in detail. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.2022-08-08C# salt+hash encryption
This article mainly introduces the C# salt+hash encryption rules, the principle of generating pseudo-random numbers in C# salt, the principle of hash, the reasons for using hash for encryption, etc. It has certain reference value, let's take a look with the editor below2017-01-01Example of the function of simple integer four-step operation calculator implemented by C#
This article mainly introduces the functions of the simple integer four-step calculation calculator implemented in C#, involving related operation techniques such as C# interface layout, event response and numerical operations. Friends who need it can refer to it2017-09-09Sample code of private members and methods in C# access and call classes
Accessing private members of a class is not a good practice. Everyone knows that private members cannot be accessed externally. This article mainly introduces relevant information about C# access and calling private members and methods in the class. The article introduces the example code in detail. Friends who need it can refer to it.2018-06-06Analysis of the difference between overloading and rewriting in C#
This article mainly introduces the difference between reloading and rewriting in C# in detail. Interested friends can refer to it.2016-02-02Detailed explanation of XML and JSON data processing cases in C#
In modern software development, the demand for data exchange and storage is growing, and XML and JSON have become the two most commonly used data formats. They have their own characteristics and have their own advantages in different scenarios. This article will discuss how to deal with these two data formats from the perspective of C# and share some common problems and solutions. Friends who need it can refer to it.2024-09-09Unity UGUI's StandaloneInputModule Standard Input Module Component Use Example
This article mainly introduces the detailed explanation of the use examples of the StandaloneInputModule standard input module component of Unity UGUI. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get promoted as soon as possible to get a salary increase.2023-08-08C# development WeChat portal and application (5) User group information management
This article mainly introduces the fifth article of C# development WeChat portal and application. User group information management has certain reference value. Interested friends can refer to it.2017-06-06