C# SMTP 无法在 Outlook.com 端口 587 上进行身份验证。“服务器响应为:5.7.1 客户端未通过身份验证”
Posted
技术标签:
【中文标题】C# SMTP 无法在 Outlook.com 端口 587 上进行身份验证。“服务器响应为:5.7.1 客户端未通过身份验证”【英文标题】:C# SMTP fails to authenticate on Outlook.com, port 587. "The server response was: 5.7.1 Client was not authenticated" 【发布时间】:2012-12-10 21:26:58 【问题描述】:我正在尝试发送自动电子邮件(真正需要的商业原因 - 不是垃圾邮件!)。与以下类似的代码曾经与另一家邮件服务提供商合作,但客户已转移到“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
这个 (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();
请注意 DeliveryMethod 和 EnableSsl 正在设置并且端口是 587。
我在我的 Outlook 客户端(桌面程序,而不是 Web 服务)上创建了另一个测试帐户来测试上面代码使用的参数:
用户名 密码 smtp.outlook.com 587 TLS并且 Outlook 中的“测试帐户设置”显示参数正确(“任务:发送测试电子邮件 - 状态:已完成”)。所以我认为用户名和密码一定是正确的。
我已打开详细登录,如下所述: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()
这些行中的第一行向我表明 SmtpClient 和 outlook.com 已就要使用的加密协议达成一致,但其余部分对我来说意义不大,除非“5.7.1 客户端未通过身份验证”行。
有没有人在连接到 Outlook.com 时遇到过类似的问题?
提前致谢。
罗斯
【问题讨论】:
【参考方案1】:我知道这听起来很奇怪,但 AFAI 记得你应该在设置 Credentials
之前使用 UseDefaultCredentials = false
。将您的代码更改为:
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(From, Password);
【讨论】:
我简直不敢相信,但你是对的!我已经浪费了一上午了,grrr.... 是的,教训很辛苦。我明白你的意思。我确实能感觉到:) 天哪。我整天都在听这个。谢谢!! @jeroenh :很抱歉听到你被这个发现了。我很高兴地发现我不是唯一一个。正如他们所说,“苦难爱陪伴!” 什么是AFAI?!没看懂【参考方案2】:注释掉 UseDefaultCredentials 对我有用,请参阅 Microsoft Bug: https://connect.microsoft.com/VisualStudio/feedback/details/725279/smtp-tls-authentication-usedefaultcredentials-bug-using-office365-server-as-smart-host
【讨论】:
【参考方案3】:如果您的 Microsoft 帐户已启用两步验证,请确保您使用 应用密码(可以创建 here)。
【讨论】:
以上是关于C# SMTP 无法在 Outlook.com 端口 587 上进行身份验证。“服务器响应为:5.7.1 客户端未通过身份验证”的主要内容,如果未能解决你的问题,请参考以下文章
mailcore2 通过 SMTP Outlook.com 给出 Error Domain=MCOErrorDomain Code=1 “无法建立与服务器的稳定连接。”