无法使用c#SmtpClient从Office 365发送电子邮件

问题描述 投票:1回答:1

[尝试使用SmtpClient从c#中的Winforms应用发送电子邮件。通过该MS article,它应该可以工作。我搜索了很多论坛等,但没有找到任何解决方案。该错误消息似乎表明客户端未通过身份验证。密码是正确的,我已经以该用户身份使用密码登录并可以使用。 2FA已启用,但htis应该不会妨碍它?

我检查过的东西就位

  1. 启用SSl
  2. 来自电子邮件和用户电子邮件是相同的
  3. 端口587
  4. 我的帐户中有这个帐户,并且可以正常发送
  5. 防火墙未阻止此端口。我还有其他代码使用与非O365主机完全相同的代码,并且效果很好

代码

            var userName = "[email protected]";
            var password = "password";
            var msg = new MailMessage();
            msg.To.Add(new MailAddress("[email protected]"));
            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);

Exception

  • SMTP服务器需要安全的连接,或者客户端没有已验证。服务器响应为:5.7.57 SMTP;客户不是经过验证可在MAIL FROM]期间发送匿名邮件
  • 邮件工具包的实现

(建议vasily.sib)
var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test User", "[email protected]"));
        message.To.Add(new MailboxAddress("Gmail User", "[email protected]"));
        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("[email protected]", "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]

[尝试使用SmtpClient从c#中的Winforms应用发送电子邮件。通过这篇MS文章,它应该可以工作。我搜索了很多论坛等,但没有找到任何解决方案。错误消息...

c# office365 smtpclient
1个回答
0
投票

已解决。正是2FA阻止了它的发生。把它关掉就可以了

© www.soinside.com 2019 - 2024. All rights reserved.