gpt4 book ai didi

c# - 如何通过 Yandex SMTP 发送电子邮件 (C# ASP.NET)

转载 作者:行者123 更新时间:2023-12-01 23:12:06 25 4
gpt4 key购买 nike

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

最佳答案

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

  • 设置 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 上发布了帖子。。因此,我认为回答这个问题的学分应该归于他们。

关于c# - 如何通过 Yandex SMTP 发送电子邮件 (C# ASP.NET),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34808365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com