Tuesday, September 23, 2014

Important change in Yandex SMTP settings when send emails via .Net SmtpClient

If you use smtp.yandex.ru server for sending emails in .Net application via standard System.Net.Mail.SmtpClient class then starting from recent time you could faced with the problem that emails are not sent anymore. The problem is caused by the change in Yandex SMTP configuration: starting from 16 September 2014 it works only via SSL. According to information from company’s site the following settings should be used:

Server: smtp.yandex.ru
Port: 465
Enable SSL: yes

However if you will use these settings in .Net application, emails still won’t work (you will get timeout exception):

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

In order to make it work we still need to use standard SMTP port 25 (not 465 as mentioned in mail clients settings page in Yandex help) and set EnableSsl = true:

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

Probably this behavior is related with realization of “explicit SSL” mode in SmtpClient when connection is established via 25 port in non-encrypted form and then switches to secure mode. Anyway as it is not obvious solution I decided to post it here, probably it will help someone.

1 comment:

  1. You saved me a lot of trouble. Thanks you very much.

    ReplyDelete