【发布时间】:2020-03-20 08:06:01
【问题描述】:
尝试使用 SmtpClient 在 c# 中从我的 winforms 应用程序发送电子邮件。通过这个 MS article 它应该可以工作。我搜索了许多论坛等,但没有找到任何解决方案。该错误消息似乎表明客户端未通过身份验证。密码是正确的 我已经以这个用户的身份登录了密码和它的罚款。 2FA 已开启,但 htis 不应该妨碍它吗?
我检查的东西已经到位
- 启用 SSl
- 发件人邮箱和用户邮箱是一样的
- 587 端口
- 我在 Outlook 中有这个帐户,它发送得很好
- 防火墙未阻止此端口。我有其他代码使用与非 O365 主机完全相同的代码,它工作得很好
代码
var userName = "user@domain.onmicrosoft.com";
var password = "password";
var msg = new MailMessage();
msg.To.Add(new MailAddress("user@gmail.com"));
msg.From = new MailAddress(userName);
msg.Subject = "Test Office 365 Account";
msg.Body = "Testing email using Office 365 account.";
msg.IsBodyHtml = true;
var client = new SmtpClient{
Host = "smtp.office365.com",
Credentials = new System.Net.NetworkCredential(userName, password),
Port = 587,
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true
};
client.Send(msg);
例外
- SMTP 服务器需要安全连接或客户端没有 认证。服务器响应为:5.7.57 SMTP;客户不是 在 MAIL FROM 期间通过身份验证发送匿名邮件
MAIL KIT 实施(vasily.sib 的建议)
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Test User", "user@domain.onmicrosoft.com"));
message.To.Add(new MailboxAddress("Gmail User", "testuser@gmail.com"));
message.Subject = "test";
message.Body = new TextPart("plain")
{
Text = @"test"
};
var client = new SmtpClient(new ProtocolLogger("imap.log"));
// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("smtp.office365.com", 587, SecureSocketOptions.Auto); //this is fine and it connects
var clientAuthenticationMechanisms = client.AuthenticationMechanisms;
client.AuthenticationMechanisms.Remove("XOAUTH2");
// Note: only needed if the SMTP server requires authentication
client.Authenticate("user@domain.onmicrosoft.com", "password"); // this is where it fails with Authentication Failure
client.Send(message);
client.Disconnect(true);
邮件工具包日志输出
Connected to smtp://smtp.office365.com:587/?starttls=when-available
S: 220 SYCP282CA0015.outlook.office365.com Microsoft ESMTP MAIL Service ready at Mon, 25 Nov 2019 04:49:36 +0000
C: EHLO [192.168.2.50]
S: 250-SYCP282CA0015.outlook.office365.com Hello [58.6.92.82]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250 SMTPUTF8
C: STARTTLS
S: 220 2.0.0 SMTP server ready
C: EHLO [192.168.2.50]
S: 250-SYCP282CA0015.outlook.office365.com Hello [58.6.92.82]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH LOGIN XOAUTH2
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250 SMTPUTF8
C: AUTH LOGIN
REMOVED BASE64 DATA (password, login)
S: 535 5.7.3 Authentication unsuccessful [SYCP282CA0015.AUSP282.PROD.OUTLOOK.COM]
【问题讨论】:
-
微软不推荐使用System.Net.Mail.SmtpClient(见备注部分)。请改用 MailKit。
-
@vasily.sib ...试过了,但它不起作用。无法验证..我再次尝试了电子邮件和密码,它工作正常,但在代码中它无法验证..2FA 会阻止它吗?
-
您是否在任何代理服务器(如公司网络内部)下运行您的代码?
-
@RICKYKUMAR 没有。当我使用 MailKit 运行它时,我认为端口 587 上的“smtp.office365.com”很好,但是当我尝试验证它失败时。我能想到的只有用户名和密码,但它们都很好,因为我可以使用这些登录到 portal.office.com 并查看那里的收件箱
标签: c# office365 smtpclient