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

mspsb9vt  于 2023-02-01  发布在  .NET
关注(0)|答案(3)|浏览(460)

以前,我用我的服务器作为邮件主机,通过我自己的主机发送电子邮件。现在,我用Yandex作为我的邮件服务器。我试图通过Yandex SMTP发送电子邮件。但是,我不能实现它。我得到“操作已超时”每次的消息。我可以发送和接收电子邮件与相同的设置,当我使用雷鸟。因此,这个帐号没有问题。2我很感谢你的指导。3你可以看到我的代码如下:

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

btxsgosb1#

经过这么多的试验和错误,我已经找到了如何使它工作。我对问题中张贴的代码做了以下修改:

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

发送方法=系统.网络.邮件. SMTP发送方法.网络;使用默认凭据=假;
补充说明:我使用Azure服务器。我后来意识到我没有为端口465配置SMTP端点。也就是说,我必须添加上面的2行代码才能使电子邮件传递工作,仅仅更改端口是不够的。我的观点是,在做任何进一步的事情之前,值得检查Azure和防火墙上定义的端口。
我能够使我的代码工作得到帮助,从@Uwe和@Dima-Babich,@Rail谁张贴在下面的页面Yandex smtp settings with ssl。因此,我认为学分回答这个问题应该给他们。

pftdvrlh

pftdvrlh2#

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

fcy6dtqo

fcy6dtqo3#

我也遇到了同样的问题,我通过去Yandex邮件解决了它,然后更改了一些设置。
转到:1-设置。2-电子邮件客户端。3-设置所选的POP3设置为全部。

相关问题