We all know that System in C. net. mail. the SmtpClient class is used to send emails to the SMTP server. However, if you use a local SMTP server (such as the Local iis smtp server) to send emails, the email address of many large websites is treated as spam, because the address of the local SMTP server is not known, and the sent email is taken as an unknown email, therefore, we need to use SMTP servers of some large well-known websites to send emails to us. Here we will introduce how to use the SMTP server 163 to send emails.
PS: if you use the 163 SMTP server to send emails, the 163 email address will be used as the sender's address for all emails sent by your development system, however, I think this is better than sending emails to users' spam every time, because if all the emails you send are spam emails, the user may not see what you send at all, delete the email, which is the worst ....
Let's take a look at the Code:
System. Net. Mail. SmtpClient client = new System. Net. Mail. SmtpClient ();
Client. Host = "smtp.163.com"; // use the 163 SMTP server to send an email.
Client. usedefacrecredentials = true;
Client. DeliveryMethod = System. Net. Mail. SmtpDeliveryMethod. Network;
Client. credentials = new System. net. networkCredential ("abc", "********"); // The SMTP server of 163 must use the username and password of the 163 mailbox for authentication, if you do not need to apply for 163 applications,
// Assume that you already have an account with a 163 email address. The username is abc and the password is *******
System. Net. Mail. MailMessage Message = new System. Net. Mail. MailMessage ();
Message. from = new System. net. mail. mailAddress ("abc@163.com"); // note that 163 appears to require the sender's email address to be 163, 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 for SMTP Server Authentication, so the sender's email address here should also be written as a abc@163.com
Message. To. Add ("123456@gmail.com"); // send the mail To Gmail
Message. To. Add ("123456@qq.com"); // send the email To the QQ mailbox
Message. Subject = "test ";
Message. Body = "test email Body ";
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 );