C# SMTP 无法在 Outlook.com 的 587 端口上进行身份验证。“服务器响应为:5.7.1 客户端未通过身份验证”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14021649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:26:29  来源:igfitidea点击:

C# SMTP fails to authenticate on Outlook.com, port 587. "The server response was: 5.7.1 Client was not authenticated"

c#smtpsslsmtpclientoutlook.com

提问by RAM

I'm attempting to send automated emails (genuinely required business reason - not spam!). Code similar to that below used to work with another mail service provider but the customer has moved to "outlook.com" and they're now getting:

我正在尝试发送自动电子邮件(真正需要的业务原因 - 不是垃圾邮件!)。类似于下面的代码曾经与另一个邮件服务提供商合作,但客户已转移到“outlook.com”,他们现在得到:

    The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated

Surely this (C#) code should work:

当然,这个(C#)代码应该可以工作:

    private void Send_Click(object sender, EventArgs e)
    {
        MailMessage message = null;
        try
        {
            message = new MailMessage(From, To);
            message.Subject = "Update Request Session from " + From;
            message.CC.Add(Cc);
            message.Body = "Test message: please ignore.";
            message.IsBodyHtml = false;

            SmtpClient client = new SmtpClient("smtp.outlook.com", 587);
            client.Credentials = new System.Net.NetworkCredential(From, Password);
            client.UseDefaultCredentials = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;

            client.Send(message);
        }
        catch (Exception ex)
        {
            Status.Text = ex.Message;
        }

        if (message != null)
        {
            message.Dispose();
        }
    }

Please note the DeliveryMethod and EnableSsl are being set and that the port is 587.

请注意正在设置 DeliveryMethod 和 EnableSsl,并且端口为 587。

I've created another test account on my Outlook client (the desktop program, not the web service) to test the parameters which the code above uses:

我在 Outlook 客户端(桌面程序,而不是 Web 服务)上创建了另一个测试帐户来测试上述代码使用的参数:

  • user name
  • password
  • smtp.outlook.com
  • 587
  • TLS
  • 用户名
  • 密码
  • smtp.outlook.com
  • 587
  • TLS

and the "Test Account Settings" in Outlook shows that the parameters are correct ("Task: Send test e-mail message - Status:Completed"). So I think the the user name and password must be correct.

Outlook 中的“测试帐户设置”显示参数正确(“任务:发送测试电子邮件 - 状态:已完成”)。所以我认为用户名和密码一定是正确的。

I've turned on verbose logging on as described here: http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/28b5a576-0da2-42c9-8de3-f2bd1f30ded4/. This has produced a plenty of output, too much to post here. Here are the last few lines:

我已按照此处所述打开详细登录:http: //social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/28b5a576-0da2-42c9-8de3-f2bd1f30ded4/。这已经产生了大量的输出,太多了,无法在这里发布。这是最后几行:

System.Net Information: 0 : [6312] SecureChannel#28756230 - Remote certificate was verified as valid by the user.
System.Net.Sockets Verbose: 0 : [6312] Socket#59109011::Send()
System.Net.Sockets Verbose: 0 : [6312] Data from Socket#59109011::Send
System.Net.Sockets Verbose: 0 : [6312] 00000000 : 17 03 01 00 30 4B BC 72-D3 A8 D7 EC FB 3C 21 0F : ....0K.r.....<!.
System.Net.Sockets Verbose: 0 : [6312] 00000010 : D7 BA A2 1F 72 1D D0 E6-4B DB 9A EC 9E 70 FD BC : ....r...K....p..
System.Net.Sockets Verbose: 0 : [6312] 00000020 : 73 B9 CC 6A 11 9E 32 E0-B2 47 D7 35 C0 BA CD DE : s..j..2..G.5....
System.Net.Sockets Verbose: 0 : [6312] 00000030 : 38 CA F6 93 8E                                  : 8....
System.Net.Sockets Verbose: 0 : [6312] Exiting Socket#59109011::Send()  -> 53#53
System.Net.Sockets Verbose: 0 : [6312] Socket#59109011::Receive()
System.Net.Sockets Verbose: 0 : [6312] Data from Socket#59109011::Receive
System.Net.Sockets Verbose: 0 : [6312] 00000000 : 17 03 01 00 D0                                  : .....
System.Net.Sockets Verbose: 0 : [6312] Exiting Socket#59109011::Receive()   -> 5#5
System.Net.Sockets Verbose: 0 : [6312] Socket#59109011::Receive()
System.Net.Sockets Verbose: 0 : [6312] Data from Socket#59109011::Receive
System.Net.Sockets Verbose: 0 : [6312] 00000005 : 4D 6F 22 3C B8 A4 39 14-B0 FC B9 58 23 9A 26 70 : Mo"<..9....X#.&p
System.Net.Sockets Verbose: 0 : [6312] 00000015 : 89 52 B4 E1 8D 74 D6 23-5C 2B 7C 44 6E EB 1A F7 : .R...t.#\+|Dn...
System.Net.Sockets Verbose: 0 : [6312] 00000025 : 7A 56 3D 18 0E 93 9C C5-C6 43 28 25 AA 3D DF 89 : zV=......C(%.=..
System.Net.Sockets Verbose: 0 : [6312] 00000035 : 46 78 9B 69 ED FA 51 65-29 D9 DF A6 3F 9F 9F 25 : Fx.i..Qe)...?..%
System.Net.Sockets Verbose: 0 : [6312] 00000045 : 22 18 99 13 6A AA FB 49-BE 9C 53 F2 96 3C 5A 5A : "...j..I..S..<ZZ
System.Net.Sockets Verbose: 0 : [6312] 00000055 : 60 E1 23 FA CA 92 B9 4A-75 9D 87 D5 66 4D FB 84 : `.#....Ju...fM..
System.Net.Sockets Verbose: 0 : [6312] 00000065 : 99 63 D1 27 C9 41 90 C8-3D BE 82 E6 A2 62 EB 56 : .c.'.A..=....b.V
System.Net.Sockets Verbose: 0 : [6312] 00000075 : 5E D0 ED 67 B3 67 FD 6A-B0 99 34 07 DA 88 3D 02 : ^..g.g.j..4...=.
System.Net.Sockets Verbose: 0 : [6312] 00000085 : 2E E6 E5 DB 4F E7 C0 EF-A9 AD 01 C0 5D FE 94 19 : ....O.......]...
System.Net.Sockets Verbose: 0 : [6312] 00000095 : 92 0A 0E 6F C3 C7 F5 62-E4 15 F2 6D 56 A4 5B 74 : ...o...b...mV.[t
System.Net.Sockets Verbose: 0 : [6312] 000000A5 : C0 7A 2F 16 A7 F2 C0 F8-F4 90 00 EB DB 52 90 85 : .z/..........R..
System.Net.Sockets Verbose: 0 : [6312] 000000B5 : ED FA C6 91 5A 32 49 4A-9F F9 1D 11 62 97 C7 DC : ....Z2IJ....b...
System.Net.Sockets Verbose: 0 : [6312] 000000C5 : 96 11 67 E0 95 D2 95 0C-D9 41 C9 81 F8 4C 58 0F : ..g......A...LX.
System.Net.Sockets Verbose: 0 : [6312] Exiting Socket#59109011::Receive()   -> 208#208
System.Net.Sockets Verbose: 0 : [6312] Socket#59109011::Send()
System.Net.Sockets Verbose: 0 : [6312] Data from Socket#59109011::Send
System.Net.Sockets Verbose: 0 : [6312] 00000000 : 17 03 01 00 40 45 FD 20-1F 27 A1 B3 50 A3 99 5D : ....@E. .'..P..]
System.Net.Sockets Verbose: 0 : [6312] 00000010 : F3 DA 03 7E 47 C4 8C 91-21 7F 25 7C 09 CC E4 68 : ...~G...!.%|...h
System.Net.Sockets Verbose: 0 : [6312] 00000020 : 27 95 BE C7 FB 9E BC 65-67 64 CE C7 BE 4A BB 48 : '......egd...J.H
System.Net.Sockets Verbose: 0 : [6312] 00000030 : 89 36 A4 A4 90 B4 7B B4-C0 B5 DB 5B 18 3F 4A C3 : .6....{....[.?J.
System.Net.Sockets Verbose: 0 : [6312] 00000040 : A2 DB 51 B5 46                                  : ..Q.F
System.Net.Sockets Verbose: 0 : [6312] Exiting Socket#59109011::Send()  -> 69#69
System.Net.Sockets Verbose: 0 : [6312] Socket#59109011::Receive()
System.Net.Sockets Verbose: 0 : [6312] Data from Socket#59109011::Receive
System.Net.Sockets Verbose: 0 : [6312] 00000000 : 17 03 01 00 40                                  : ....@
System.Net.Sockets Verbose: 0 : [6312] Exiting Socket#59109011::Receive()   -> 5#5
System.Net.Sockets Verbose: 0 : [6312] Socket#59109011::Receive()
System.Net.Sockets Verbose: 0 : [6312] Data from Socket#59109011::Receive
System.Net.Sockets Verbose: 0 : [6312] 00000005 : 43 8E E7 44 71 B3 84 BC-5F 08 39 CE B1 3A A4 F9 : C..Dq..._.9..:..
System.Net.Sockets Verbose: 0 : [6312] 00000015 : F4 92 BE B3 D7 10 81 14-B9 5B BA E3 35 93 34 12 : .........[..5.4.
System.Net.Sockets Verbose: 0 : [6312] 00000025 : AF 32 F1 69 82 BD B9 8B-4C 2F 47 5F 8B 95 7E 52 : .2.i....L/G_..~R
System.Net.Sockets Verbose: 0 : [6312] 00000035 : 94 4C A6 79 6A F6 E2 84-D1 9E A5 D3 16 98 44 E1 : .L.yj.........D.
System.Net.Sockets Verbose: 0 : [6312] Exiting Socket#59109011::Receive()   -> 64#64
System.Net Error: 0 : [6312] Exception in the SmtpClient#44419000::Send - The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
System.Net Error: 0 : [6312]    at     System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
   at     System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
   at     System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
   at     System.Net.Mail.SmtpClient.Send(MailMessage message)
System.Net.Sockets Verbose: 0 : [6312] Socket#59109011::Dispose()
System.Net Verbose: 0 : [6312] Exiting SmtpClient#44419000::Send() 

The first of those lines suggests to me that the SmtpClient and outlook.com have agreed on the encryption protocols to use, but the rest of it is means little to me bar the "5.7.1 Client was not authenticated" line.

这些行中的第一行向我表明 SmtpClient 和 Outlook.com 已就要使用的加密协议达成一致,但其余部分对我来说意义不大,除非“5.7.1 客户端未通过身份验证”行。

Has anyone run into similar problems connecting to Outlook.com?

有没有人遇到过连接 Outlook.com 的类似问题?

Thanks in advance.

提前致谢。

Ross

罗斯

采纳答案by Arash Milani

I know that it sounds weird but AFAI remember you should use the UseDefaultCredentials = falsebefore setting Credentials. change your code to:

我知道这听起来很奇怪,但 AFAI 记得你应该使用UseDefaultCredentials = falsebefore 设置Credentials。将您的代码更改为:

client.UseDefaultCredentials = false;    
client.Credentials = new System.Net.NetworkCredential(From, Password);

回答by White hawk

Also make sure that you use an app password(can be created here) if 2-step authentication has been enabled for your Microsoft account.

如果您的 Microsoft 帐户已启用两步身份验证,还请确保您使用应用程序密码(可以在此处创建)。