下面的代碼如果使用80 端口沒問題,但是 端口 用 465 跟587 就出問題。
Inner Exception 1:
IOException: Unable to read data from the transport connection: The connection was closed.
public void SendEmail(string email, string subject, string content)
{
if (!SmtpServerEnabled) {
return;
}
SmtpClient client = new SmtpClient(this.SmtpServer,587)
{
//UseDefaultCredentials = false,
Credentials = new NetworkCredential(this.SmtpUserName,this.SmtpPassword)
};
client.EnableSsl = true;
MailMessage mailMessage = new MailMessage
{
From = new MailAddress(this.SmtpSender)
};
mailMessage.From = new MailAddress(this.SmtpUserName);
mailMessage.To.Add(email);
mailMessage.IsBodyHtml = true;
mailMessage.Body = content;
mailMessage.Subject = subject;
try
{
client.Send(mailMessage);
}
catch (System.Exception e) {
System.Console.WriteLine(e.InnerException?.Message);
}
}