Home .NET Using SmtpClient to send mail via Yandex SMTP server with SSL

Using SmtpClient to send mail via Yandex SMTP server with SSL

by admin

If you use Yandex.Mail for your domain, you probably already know that a week ago from September 16, 2014. Yandex’s SMTP server smtp.yandex.ru completely switched to SSL, which the company conscientiously notified customers about (I say without any sarcasm, they really worked in good faith). The mailing list included instructions for popular email clients on what changes they should make to make the mail work after switching to SSL: Encryption of transmitted data In short, you have to specify port 465 in the SMTP settings and enable SSL encryption. However, if you have your own .Net application which uses the standard class System.Net.Mail.SmtpClient , an exception will be thrown with a timeout message when you try to use these instructions.
After reading the above instructions, we would expect the following code to work without any problems :

var msg = new MailMessage(from, to, subj, body);var smtpClient = new SmtpClient("smtp.yandex.ru", 465);smtpClient.Credentials = new NetworkCredential(username, pwd);smtpClient.EnableSsl = true;smtpClient.Send(msg);

However, as mentioned above, an exception is thrown when you try to send an email. You still need to use the standard SMTP port 25 as for unsecured connections but specify EnableSsl = true to make the code work:

var msg = new MailMessage(from, to, subj, body);var smtpClient = new SmtpClient("smtp.yandex.ru", 25);smtpClient.Credentials = new NetworkCredential(username, pwd);smtpClient.EnableSsl = true;smtpClient.Send(msg);

This is probably due to the implementation of explicit SSL mode in SmtpClient, where the connection is established through port 25 in unencrypted form and then switched to secured mode. Nevertheless, since the solution is not obvious, I decided to publish it to save time for those who face this problem.

You may also like