无法通过 smtp.office365.com 发送电子邮件 ASP.NET MVC

发布于 2025-01-20 01:30:52 字数 1152 浏览 3 评论 0原文

我读了很多博客&有关此主题的教程,但仍然不起作用。我正在尝试使用Office365的SMTP服务器发送电子邮件。

这是我的代码,

SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;  
smtpClient.Credentials = new System.Net.NetworkCredential("EmailAddress", "Password", "domain.com");
smtpClient.Port = 587; // 25 587
smtpClient.Host = "smtp.office365.com";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Timeout = 600000;

MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("fromemailaddress", "Enquiry");
mailMessage.To.Add(new MailAddress("toemailaddress", "Test"));
mailMessage.Subject = "Inquiry";
mailMessage.Body = "\r\nName:" + user.Name + "\r\nMessage:" + user.Message
                   + "\r\nContact No.:" + user.MobileNo + "\r\nEmail Address:" + user.Email;
mailMessage.IsBodyHtml = true;

smtpClient.Send(mailMessage);

我有一个例外:

无法连接到远程服务器
失败发送邮件。
连接尝试失败了,因为连接的方在一段时间后没有正确响应,或者建立的连接失败了,因为连接的主机未能响应51.92.122.216:587

我是新手。我尝试在侦听它的范围内ping,并以相同的凭据在浏览器中成功登录。

通过Office 365登录后,我缺少任何东西?

谢谢

I've read a lot of blogs & tutorials on this subject, but it still doesn't work. I'm attempting to send email using Office365's SMTP server.

This is my code

SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;  
smtpClient.Credentials = new System.Net.NetworkCredential("EmailAddress", "Password", "domain.com");
smtpClient.Port = 587; // 25 587
smtpClient.Host = "smtp.office365.com";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Timeout = 600000;

MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("fromemailaddress", "Enquiry");
mailMessage.To.Add(new MailAddress("toemailaddress", "Test"));
mailMessage.Subject = "Inquiry";
mailMessage.Body = "\r\nName:" + user.Name + "\r\nMessage:" + user.Message
                   + "\r\nContact No.:" + user.MobileNo + "\r\nEmail Address:" + user.Email;
mailMessage.IsBodyHtml = true;

smtpClient.Send(mailMessage);

I got an exception:

Unable to connect to the remote server
Failure sending mail.
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 51.92.122.216:587

I am a newbie in this. I try to ping the domain it's listening and I successfully login in the browser with same credentials.

Anything I am missing to set up after login via Office 365?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2025-01-27 01:30:52

您的代码看起来不错,只要您遵循这些指南(正确的主机和端口)。你应该没问题。

但请注意,smtp 默认情况下处于禁用状态,您必须为您的帐户启用它。

此外,如果您配置了 MFA(令牌或文本),那么您就不走运了,因为它不会接受您的密码。如果是这种情况,您需要创建一个特殊的应用密码,如下所述这里

You code looks good, and as long as you have followed these guides (correct host and ports). You should be fine.

But be aware, smtp is disabled by default, and you have to enable it for your account.

Also if you have configured MFA (so a token or a text) you’re out off luck, since it won’t accept your password. If that is the case you need to create a special app password as explained here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文