Ali cloud solutions overseas servers disable port 25 outgoing messages

Hong Kong found that when using Ali cloud mail server fails, the inspection found Ali cloud server disabled 25 Hong Kong port
solution is to use other ports, such as 465, using the ssl encryption
attempt to change the code to send the message:

using(var client = new SmtpClient())
{
	client.EnableSsl = true;
	client.Port = 465;
	......
       client.Send(msg);
}

In this way or not, reported that the operation has timed out error, upon inquiry, System.Net.Mail support Explicit SSL but do not support Implicit SSL
try to use System.Web.Mail, suggesting "library obsolete."

End-use MailKit library solve the problem:

Nuget installation package

PM>Install-Package MailKit -Version 2.2.0

 

 1 using System;
 2 using MimeKit;
 3 using MailKit.Net.Smtp;
 4 using System.Threading.Tasks;
 5 
 6 namespace MyWebApp
 7 {
 8     public class EmailSender
 9     {
10         private string _displayname = "Your Name";
11         private string _from = "[email protected]";
12         private string _host = "smtp.aliyun.com";
13         private int _port = 465;
14         private string _password = "xxxxxx";
15         private bool _enablessl = true;
16  
17         public void SendEmail(string to, string subject, string body)
18         {
19             var message = new MimeMessage();
20             message.From.Add(new MailboxAddress(_displayname, _from));
21             message.To.Add(new MailboxAddress(to));
22             message.Subject = subject;
23             message.Body = new TextPart("html") { Text = body };
24             using (var client = new SmtpClient())
25             {
27                 client.ServerCertificateValidationCallback = (s, c, h, e) => true;
28                 client.Connect(_host, _port, _enablessl);
30                 client.Authenticate(_from, _password);
31                 client.Send(message);
32                 client.Disconnect(true);
33             }
34         }55     }
56 }

 

Guess you like

Origin www.cnblogs.com/redpod/p/mailkit.html