How to Send Email via Yandex SMTP (C# ASP.NET)
Asked Answered
D

3

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 protected]";
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);
Deena answered 15/1, 2016 at 9:51 Comment(4)
Are you using a setting which is called something like "Authenticate with pop3 first"?Wimsatt
Do you have a firewall that is preventing your own code from going outside?Wimsatt
@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.Deena
Before you set credentials try: SMTP.UseDefaultCredentials = falseWimsatt
D
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.

Deena answered 18/1, 2016 at 8:45 Comment(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. :)Elsi
K
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.

Kinson answered 26/2, 2018 at 18:57 Comment(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 ReviewPrelatism
Thanks for the explanation.Will add more details.Kinson
D
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.

Dismay answered 30/1, 2023 at 22:22 Comment(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.Normy

© 2022 - 2024 — McMap. All rights reserved.