无法通过 smtp.office365.com 发送电子邮件 ASP.NET MVC
我读了很多博客&有关此主题的教程,但仍然不起作用。我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码看起来不错,只要您遵循这些指南(正确的主机和端口)。你应该没问题。
但请注意,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.