8

Formerly, I used my server as mail host and was sending emails via my own host. Now, I use Yandex as my mail server. I'm trying to send emails via Yandex SMTP. However, I could not achieve it. I get "the operation has timed out" message every time. I'm able to send & receive email with the same settings when I use Thunderbird. Hence, there is no issue with the account. I appreciate your guidance. You can see my code below:

EmailCredentials credentials = new EmailCredentials();
credentials.Domain = "domain.com";
credentials.SMTPUser = "email@domain.com";
credentials.SMTPPassword = "password";
int SmtpPort = 465;
string SmtpServer = "smtp.yandex.com";

System.Net.Mail.MailAddress sender = new System.Net.Mail.MailAddress(senderMail, senderName, System.Text.Encoding.UTF8);

System.Net.Mail.MailAddress recipient = new System.Net.Mail.MailAddress(recipientEmail, recipientName, System.Text.Encoding.UTF8);

System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage(sender, recipient);

email.BodyEncoding = System.Text.Encoding.UTF8;
email.SubjectEncoding = System.Text.Encoding.UTF8;

System.Net.Mail.AlternateView plainView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(System.Text.RegularExpressions.Regex.Replace(mailBody, @"<(.|\n)*?>", string.Empty), null, MediaTypeNames.Text.Plain);

System.Net.Mail.AlternateView htmlView =  System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailBody, null, MediaTypeNames.Text.Html);

email.AlternateViews.Clear();
email.AlternateViews.Add(plainView);
email.AlternateViews.Add(htmlView);
email.Subject = mailTitle;

System.Net.Mail.SmtpClient SMTP = new System.Net.Mail.SmtpClient();
SMTP.Host = SmtpServer;
SMTP.Port = SmtpPort;
SMTP.EnableSsl = true;
SMTP.Credentials = new System.Net.NetworkCredential(credentials.SMTPUser, credentials.SMTPPassword);

SMTP.Send(email);
Nedim
  • 676
  • 1
  • 7
  • 9
  • Are you using a setting which is called something like "Authenticate with pop3 first"? – Uwe Hafner Jan 15 '16 at 11:58
  • Do you have a firewall that is preventing your own code from going outside? – Uwe Hafner Jan 15 '16 at 12:04
  • 1
    @Uwe I don't use "Authenticate with pop3 first" or anything related to pop3. I have a firewall on server. I checked the settings for outbound rules. I see that TCP ports are defined only for 25,110,143,587. It does not have 465. I will add it and try again. However, the code does not work on my local computer neither. – Nedim Jan 15 '16 at 12:10
  • Before you set credentials try: SMTP.UseDefaultCredentials = false – Uwe Hafner Jan 15 '16 at 13:56

3 Answers3

20

After so many trials & errors, I have found how to make it work. I have made the following changes on the code posted in the question:

  • Set SmtpPort = 587
  • Added the following 2 lines of code:

    SMTP.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; SMTP.UseDefaultCredentials = false;

Additional note: I use Azure server. I realized later that I did not configure the smtp endpoint for port 465. That being said, I had to add the 2 lines of code above in order to make email delivery work, just changing the port was not enough. My point is it is worth to check the defined ports on Azure and firewall before doing anything further.

I was able to make my code work by getting help from @Uwe and also @Dima-Babich, @Rail who posted on the following page Yandex smtp settings with ssl . Hence, I think credits to answer this question should go to them.

Community
  • 1
  • 1
Nedim
  • 676
  • 1
  • 7
  • 9
  • 1
    Thank you bro. You saved my day but I tried only changing SmtpPort = 587 not other things and it worked. No need to add other lines but I dont know. Well I dont use Azure server so you can be right if you use Azure servers. I just wanted to share. Thank you again. :) – Kemal Can ÖZÇELİK Mar 12 '18 at 14:28
1

Try using port 25 instead of 465 specified in Yandex help. I found this info on https://habrahabr.ru/post/237899/. They mentioned that it might be due to the fact that explicit SSL mode was implemented in the SmtpClient. Then port 25 is used for establishing connection in unencrypted mode and after that, protected mode is switched on.

husky
  • 87
  • 5
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/18941537) – Enrique Moreno Tent Feb 26 '18 at 20:51
  • Thanks for the explanation.Will add more details. – husky Feb 28 '18 at 09:07
0

I had the same problem. I solved it by going to the Yandex mail, and then change some settings.

Go to: 1- Settings. 2- Email clients. 3- Set selected POP3 setting that is all.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – MD. RAKIB HASAN Feb 01 '23 at 10:17