简体   繁体   English

如何通过 Yandex SMTP 发送 Email (C# ASP.NET)

[英]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.现在,我使用 Yandex 作为我的邮件服务器。 I'm trying to send emails via Yandex SMTP. However, I could not achieve it.我正在尝试通过 Yandex SMTP 发送电子邮件。但是,我无法实现。 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.当我使用 Thunderbird 时,我可以使用相同的设置发送和接收 email。 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);

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 设置SmtpPort = 587
  • Added the following 2 lines of code: 添加了以下2行代码:

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

Additional note: I use Azure server. 附加说明:我使用Azure服务器。 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. 我后来意识到我没有为端口465配置smtp端点。话虽这么说,我必须在上面添加2行代码才能使电子邮件传递工作,只是更改端口是不够的。 My point is it is worth to check the defined ports on Azure and firewall before doing anything further. 我的观点是,在进一步操作之前,检查Azure和防火墙上定义的端口是值得的。

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 . 我能够通过@Uwe和@Dima-Babich,@ Rail的帮助使我的代码工作,他们在下面的页面上发布了ssl的Yandex smtp设置 Hence, I think credits to answer this question should go to them. 因此,我认为回答这个问题应该归功于他们。

Try using port 25 instead of 465 specified in Yandex help. 尝试使用端口25而不是Yandex帮助中指定的465。 I found this info on https://habrahabr.ru/post/237899/ . 我在https://habrahabr.ru/post/237899/上找到了这个信息。 They mentioned that it might be due to the fact that explicit SSL mode was implemented in the SmtpClient. 他们提到可能是因为在SmtpClient中实现了显式SSL模式。 Then port 25 is used for establishing connection in unencrypted mode and after that, protected mode is switched on. 然后,端口25用于在未加密模式下建立连接,之后,保护模式被打开。

I had the same problem.我有同样的问题。 I solved it by going to the Yandex mail, and then change some settings.我通过转到 Yandex 邮件解决了这个问题,然后更改了一些设置。

Go to: 1- Settings. Go 至:1- 设置。 2- Email clients. 2- Email 客户。 3- Set selected POP3 setting that is all. 3- 设置所有选定的 POP3 设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM