【发布时间】:2020-11-03 02:15:17
【问题描述】:
我正在尝试从 ASP.NET Core 3.1 应用程序发送邮件,但我总是收到此错误消息。我该如何解决这个问题?
[我找到了一个与此类似的answer,但答案是 9 岁,对我没有帮助。]
// Exception e;
// e.Message + e.InnerException + e.GetBaseException() =
Failure sending mail.
System.IO.IOException: Unable to read data from the transport connection:
The connection was closed. at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer,
Int32 offset, Int32 read, Boolean readLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader
caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) System.IO.IOException: Unable to read data from the transport connection: The connection was closed.
at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at
System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at
System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at
System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)
这是我的邮件发送代码 -
private void sendmail(string usermail, string message)
{
SmtpClient smtpClient = new SmtpClient("smtp.yandex.com", 465);
smtpClient.Credentials = new System.Net.NetworkCredential("may mail address", "password");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.Body = message;
mail.Subject = "My Awesome Subject";
mail.From = new MailAddress("may mail address", "Awesome User");
mail.To.Add(new MailAddress(usermail));
smtpClient.Send(mail);
}
【问题讨论】:
-
如果你尝试使用端口 587 而不是 465 会怎样?
标签: c# asp.net .net email smtp