【问题标题】:How to Send Email via Yandex SMTP (C# ASP.NET)如何通过 Yandex SMTP (C# ASP.NET) 发送电子邮件
【发布时间】:2016-04-20 21:17:49
【问题描述】:

以前,我使用我的服务器作为邮件主机,并通过我自己的主机发送电子邮件。现在,我使用 Yandex 作为我的邮件服务器。我正在尝试通过 Yandex SMTP 发送电子邮件。但是,我无法实现它。我每次都会收到“操作已超时”的消息。当我使用 Thunderbird 时,我可以使用相同的设置发送和接收电子邮件。因此,帐户没有问题。我很感激你的指导。你可以在下面看到我的代码:

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

【问题讨论】:

  • 您是否使用了类似于“首先使用 pop3 进行身份验证”的设置?
  • 你有防火墙阻止你自己的代码出去吗?
  • @Uwe 我不使用“首先使用 pop3 进行身份验证”或与 pop3 相关的任何内容。我在服务器上有防火墙。我检查了出站规则的设置。我看到 TCP 端口仅为 25,110,143,587 定义。它没有465。我将添加它并再试一次。但是,该代码在我的本地计算机上也不起作用。
  • 在设置凭据之前尝试:SMTP.UseDefaultCredentials = false

标签: c# asp.net email smtp yandex


【解决方案1】:

经过这么多的试验和错误,我找到了如何让它发挥作用。我对问题中发布的代码进行了以下更改:

  • 设置 SmtpPort = 587
  • 添加以下两行代码:

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

补充说明: 我使用 Azure 服务器。后来我意识到我没有为端口 465 配置 smtp 端点。话虽如此,我必须添加上面的 2 行代码才能使电子邮件传递工作,仅更改端口是不够的。我的观点是,在做任何进一步的事情之前,检查 Azure 和防火墙上定义的端口是值得的。

通过获得@Uwe 和@Dima-Babich、@Rail 的帮助,我能够使我的代码工作,他们在下一页上发布了Yandex smtp settings with ssl .因此,我认为回答这个问题的功劳应该归他们所有。

【讨论】:

  • 谢谢兄弟。你拯救了我的一天,但我只尝试改变 SmtpPort = 587 而不是其他的东西,它奏效了。无需添加其他行,但我不知道。好吧,我不使用 Azure 服务器,所以如果您使用 Azure 服务器,您可能是对的。我只是想分享。再次感谢你。 :)
【解决方案2】:

尝试使用端口 25 而不是 Yandex 帮助中指定的 465。我在https://habrahabr.ru/post/237899/ 上找到了这个信息。他们提到这可能是由于在 SmtpClient 中实现了显式 SSL 模式。然后在未加密模式下使用25端口建立连接,然后开启保护模式。

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • 感谢您的解释。将添加更多详细信息。
猜你喜欢
  • 2019-05-15
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 2011-01-04
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 2014-01-21
相关资源
最近更新 更多