C # Sending emails to local services (code implementation ),
Preface
In the past few days, the billing system requires a mailbox to retrieve the password! Here we will share with you. There are many implementation methods. This blog mainly shares the methods sent by the local server.
Method
Using System. Net; using System. Net. Mail ;////// Send the verification code by email ///Public void SendMail () {System. net. mail. smtpClient client = new System. net. mail. smtpClient (); client. host = "smtp.163.com"; // use the 163 SMTP server to send the email client. usedefacrecredentials = true; client. deliveryMethod = System. net. mail. smtpDeliveryMethod. network; client. credentials = new System. net. networkCredential ("157 *** 1797@163.com", "0713yj"); // The SMTP server of 163 needs to use the username and password of 163 mailbox as authentication System. net. mail. mailMessage Messa Ge = new System. net. mail. mailMessage (); Message. from = new System. net. mail. mailAddress ("157 *** 1797@163.com", "every day to see online cafe customer service", System. text. encoding. UTF8); // note that 163 indicates that the sender's email address must be 163, in addition, the sender's email user name must be the same as the user name used for SMTP server authentication. // because the above user name abc is used for SMTP Server Authentication, so here the sender's email address should also be written as abc@163.com // Message. to. add ("123456@gmail.com"); // send mail to Gmail Message. to. add (txtEmail. text. trim (); // send the email to the QQ mail Message. subject = "retrieve password"; // mail title Message. Body = "Hello! "+ SystemVarify +" is the verification code for your identity verification, used to retrieve the login password. "; // Mail content Message. subjectEncoding = System. text. encoding. UTF8; Message. bodyEncoding = System. text. encoding. UTF8; Message. priority = System. net. mail. mailPriority. high; Message. isBodyHtml = true; client. send (Message );}
Conclusion
The sender's email password and sender content can all be written to the configuration file. For how to modify and add a configuration file, go to [C #] to read and modify the configuration file.