Я пытаюсь отправить почту из приложения ASP. NET Core 3.1, но всегда получаю это сообщение об ошибке. Как я могу это исправить?
[Я нашел ответ , похожий на этот, но ответу 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);
}