Nedim
Nedim

Reputation: 706

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

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);

Upvotes: 8

Views: 19513

Answers (3)

Ali Mashally
Ali Mashally

Reputation: 1

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.

Upvotes: 0

husky
husky

Reputation: 87

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.

Upvotes: 1

Nedim
Nedim

Reputation: 706

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.

Upvotes: 20

Related Questions