📜 ⬆️ ⬇️

Using SmtpClient to send mail via Yandex SMTP server with SSL

If you use Yandex.Mail for a domain, you probably already know that a week ago from September 16, 2014, the SMTP server of Yandex smtp.yandex.ru completely switched to SSL, which the company conscientiously notified customers (I’m talking without any sarcasm, it really worked in good faith). The mailing list provided instructions for popular email clients about what changes to them must be made for the mail to work after the switch to SSL: Encryption of the transmitted data . In short, in the SMTP settings, you must specify port 465 and enable SSL encryption. However, if you have your own .Net application, which uses the standard System.Net.Mail.SmtpClient class to send mail, then when you try to use these instructions, an exception will appear with a message about timeout.

After reading the instructions above, we might expect the following code to work without 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 noted above, an exception is generated when you try to send a letter. For the code to work, you still need to use standard SMTP port 25, as for unprotected connections, but specifying EnableSsl = true:
 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); 

Perhaps this is due to the implementation of explicit SSL mode (explicit SSL) in SmtpClient, when the connection is established through port 25 in an unencrypted form, and then switches to protected mode. However, since The solution is not obvious, I decided to publish it in order to save time for those who face this problem.

')

Source: https://habr.com/ru/post/237899/


All Articles