C # server sends the message failed

  Mail sent the relevant port

  First, that the next port messages sent: 25/465/587

  25 port

  25 port is open for the SMTP protocol service, which is the oldest of the three ports a. 25 ports are also known as message relay port because the port is often malicious use, so now this port is mainly used for forwarding messages between mail servers, and now our cloud servers such as Ali cloud cloud Tencent, etc., are disabled by default 25 ports.

  465 port

  Port 465 is SMTPS (SMTP-over-SSL) protocol services open and the SMTPS SMTP protocol is based on a variant of the protocol SSL security protocol, it inherits the highly asymmetric encryption security and reliability of the SSL security protocol, Mail to prevent leaks, SMTP and SMTPS relationship similar to the relationship between HTTP and HTTPS. 465 port has not been recognized IETF, so those companies recruit prospective Internet strict criteria may not have approved, but in the domestic environment is used as an alternative port to port 25.

  587 port

  Port 587 is the mail client submitted a message to the mail server recommended port, is STARTTLS protocol, belonging to the TLS protocol, also called the message submission port. Client submits a message through port 587 and forwards between the server through port 25, which is an ideal model.

  

 

  problem

    The following mail code  

    string host = "smtp.exmail.qq.com";//qq邮箱
    int port = 25;//25 465 587
    string from = "[email protected]";
    string to = "456789@qq.com";
    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);
    client.Send(message);

  Email above using System.Net.Mail, if you use the local environment, you can send mail, but if deployed to the server, especially the cloud server, the mail may be unable to pay, and this is probably because 25 port is disabled, you need to open the server port 25 (Tencent Ali clouds and other clouds to apply for open port 25), while the internet is recommended to use 465 port, but seemingly System.Net.Mail does not support port 465, 465 may be associated with the above said port IETF has not been recognized by the bar.

  System.Net.Mail does not support 465 port does not represent 465 ports are not available, if it is .net framework, you can use System.Web.Mail to use port 465, if it is .net core, can be used MailKit, not only support 465, also supports 25 and 587 port, you can use nuget installation MailKit    

    string host = "smtp.exmail.qq.com";//qq邮箱
    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);//465端口是ssl端口
    client.Authenticate(userName, password);
    client.Send(message);
    client.Disconnect(true);

  

Guess you like

Origin www.cnblogs.com/shanfeng1000/p/12389974.html