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

↘锁芯ラ 提交于 2019-11-27 15:45:15

问题


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:

    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:

    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.

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:

  • user name
  • password
  • 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.

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:

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.

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

Thanks in advance.

Ross


回答1:


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

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



回答2:


Comment out UseDefaultCredentials worked for me, see Microsoft Bug: https://connect.microsoft.com/VisualStudio/feedback/details/725279/smtp-tls-authentication-usedefaultcredentials-bug-using-office365-server-as-smart-host




回答3:


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



来源:https://stackoverflow.com/questions/14021649/c-sharp-smtp-fails-to-authenticate-on-outlook-com-port-587-the-server-respons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!