private static SmtpClient GetSmtpClient(SmtpServer setting) { var client = new SmtpClient() { DeliveryMethod = SmtpDeliveryMethod.Network, Host = setting.Host, EnableSsl = setting.UseSSL, Timeout = 18000000, }; if (setting.Port > 0 && setting.Port < 65536) { client.Port = setting.Port; } if (string.IsNullOrEmpty(setting.Username) || string.IsNullOrEmpty(setting.Password)) { client.UseDefaultCredentials = true; } else { var credetntial = new NetworkCredential(setting.Username, setting.Password); client.UseDefaultCredentials = false; client.Credentials = credetntial; } return(client); }
static void Main(string[] args) { var server = new SmtpServer { Host = "smtp.qq.com", Username = "******", Password = "******", Port = 25, Sender = "*****@*****.**", UseSSL = true }; var smtpClient = GetSmtpClient(server); ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true; smtpClient.Send(server.Sender, "*****@*****.**", "123", "222"); }