I'm' trying to send mail from ASP.NET Core 3.1 App, But I always get this error message. How can I fix this?
[I have found an answer similar to this but the answer is 9 years old, it didn't help me.]
// 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)
Here is my mail sending code -
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);
}