2

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);
}
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
Mahmudul Hasan
  • 798
  • 11
  • 35

2 Answers2

2

You need to change two things as follow and it should work:

  1. According to this answer: Try port 587 instead of 465. Port 465 is technically deprecated. SmtpClient smtpClient = new SmtpClient("smtp.yandex.com", 587);

  2. Since you use SSL you need to add: System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;. For more info have a look at this answer

System.Net.ServicePointManager.SecurityProtocol - This property selects the version of the Secure Sockets Layer (SSL) or Transport Layer Security (TLS) protocol to use for new connections that use the Secure Hypertext Transfer Protocol (HTTPS) scheme only; existing connections are not changed.

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
  • hi!, now I'm getting a new type of error - System.Net.Mail.SmtpException: Bad sequence of commands. The server response was: 5.5.4 Error: send AUTH command first. I have tried to follow - https://stackoverflow.com/questions/42391432/c-sharp-yandex-mail-send-error-5-5-4-error-send-auth-command-first but still getting the error. My password is correct. – Mahmudul Hasan Jul 14 '20 at 08:10
1

Yandex has a special password for external applications, so create that password for on the page https://passport.yandex.ru/profile. The second point is using the wrong port, it's strange, but Yandex uses 587 instead of 465.

Sibrin
  • 21
  • 3
  • hi!, now I'm getting a new type of error - System.Net.Mail.SmtpException: Bad sequence of commands. The server response was: 5.5.4 Error: send AUTH command first. I have tried to follow - https://stackoverflow.com/questions/42391432/c-sharp-yandex-mail-send-error-5-5-4-error-send-auth-command-first but still getting the error. My password is correct. And tried using app password too. – Mahmudul Hasan Jul 14 '20 at 08:11
  • Try to change a server URL to ru-related site: `smtp.yandex.ru`. – Sibrin Jul 14 '20 at 12:27
  • It's strange because I copied your code and changed URL and credentials and all work fine. Maybe do you have some network issues? – Sibrin Jul 16 '20 at 12:47
  • Haven't faced any other problems related to internet, so network is pretty stable. I'll give another try and get back to you. – Mahmudul Hasan Jul 16 '20 at 13:51