Case analysis of C # server sending mail failure


Error display:

I can send it locally, but I can’t send it after I deploy it to the server. The SMTP service is on.

report errors:

“{” success “: false,” message “:” error System.Net.Mail .SmtpException: Failure sending mail. —> System.Net.WebException : The remote name could not be resolved: ‘’


Mail sending related port

First of all, let’s talk about the port where the mail is sent25/465/587

25 ports

Port 25 is open for SMTP protocol service, which is the oldest of the three ports. Port 25 is also known as the message relay port. Because this port is often used maliciously, it is mainly used for message forwarding between mail servers. In addition, domestic cloud servers such as Alibaba cloud and Tencent cloud have disabled port 25 by default.

465 port

Port 465 is open for the service of SMTP over SSL protocol, which is a variant of SMTP protocol based on SSL security protocol. It inherits the high security and reliability of asymmetric encryption of SSL security protocol and can prevent e-mail leakage. The relationship between SMTP and smpps is similar to that between HTTP and HTTPS. Port 465 is not recognized by IETF, so those companies that strictly follow the Internet standard may not be recognized, but it is used as an alternative port for port 25 in domestic environment.

587 port

Port 587 is the recommended port for mail clients to submit messages to the mail server. It belongs to the starttls protocol, also known as the message submission port. The client submits the message through port 587, and then the server forwards the message through port 25. This is an ideal mode.


Such as the following email code

string host = " smtp.exmail.qq . com "; // QQ email
 int port = 25;//25 465 587
 string from = "[email protected]";
 string to = "[email protected]";
 string userName = "[email protected]";
 string password = "123456";

 MailMessage message = new MailMessage();
 message.From = new MailAddress(from);
 message.To.Add(new MailAddress(to));
 message.Body = "test body";
 message.Subject = "test subject";
 message.IsBodyHtml = true;
 message.SubjectEncoding = Encoding.UTF8;

 SmtpClient client = new SmtpClient(host, port);
 client.UseDefaultCredentials = true;
 client.Credentials = new NetworkCredential(userName, password);

The email above uses System.Net.Mail If you use the local environment, you can send mail. If you deploy to a server, especially a cloud server, you may not be able to send mail. This is probably because port 25 is disabled, so you need to open port 25 of the server (Alibaba cloud, Tencent cloud, etc.) and it is recommended to use 4 65 ports, but System.Net.Mail It seems that port 465 is not supported, probably because port 465 mentioned above is not approved by IETF.

System.Net.Mail If port 465 is not supported, it does not mean that port 465 is not available. If it is a. Net framework, it can be used System.Web.Mail To use port 465. If it is. Net core, you can use mailkit. It supports not only port 465, but also ports 25 and 587. You can use nuget to install mailkit

string host = " smtp.exmail.qq . com "; // QQ email
 int port = 465;//25 465 587
 string from = "[email protected]";
 string to = "[email protected]";
 string userName = "[email protected]";
 string password = "123456";
 var message = new MimeMessage();
 message.From.Add(new MailboxAddress(from));
 message.To.AddRange(new MailboxAddress[] { new MailboxAddress(to) });
 message.Subject = "test subject";
 var entity = new TextPart(TextFormat.Html)
 Text = "test body"
 SmtpClient client = new SmtpClient();
 client.Connect (host, port, port = = 465); // port 465 is SSL port
 client.Authenticate(userName, password);

This article about the C # server to send e-mail failure case analysis of the article is introduced here, more related to the C # server to send e-mail failure reason content, please search the previous article of developer or continue to browse the following related articles, hope you can support developer more in the future!