6

以前,我使用我的服务器作为邮件主机,并通过我自己的主机发送电子邮件。现在,我使用 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);
4

2 回答 2

17

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

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

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

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

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

于 2016-01-18T08:45:07.750 回答
1

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

于 2018-02-26T18:57:02.673 回答