Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

后端 未结 25 2406
悲&欢浪女
悲&欢浪女 2020-11-22 02:12

I am using following code to send email. The Code works correctly in my local Machine. But on Production server i am getting the error message

var fromAddres         


        
25条回答
  •  梦如初夏
    2020-11-22 02:48

    After spending a couple of hours today trying every solution here, I was still unable to get past this exact error. I have used gmail many times in this way so I knew it was something dumb, but nothing I did fixed the problem. I finally stumbled across the solution in my case so thought I would share.

    First, most of the answers above are also required, but in my case, it was a simple matter of ordering of the code while creating the SmtpClient class.

    In this first code snippet below, notice where the Credentials = creds line is located. This implementation will generate the error referenced in this question even if you have everything else set up properly.

    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient
    {
        Host = Emailer.Host,
        Port = Emailer.Port,
        Credentials = creds,
        EnableSsl = Emailer.RequireSSL,
        UseDefaultCredentials = false,
        DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
    }
    

    However, if you move the Credentials setter call to the bottom, the email will be sent without error. I made no changes to the surrounding code...ie...the username/password, etc. Clearly, either the EnableSSL, UseDefaultCredentials, or the DeliveryMethod is dependent on the Credentials being set first... I didn't test all to figure out which one it was though.

    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient
    {
        Host = Emailer.Host,
        Port = Emailer.Port,
        EnableSsl = Emailer.RequireSSL,
        UseDefaultCredentials = false,
        DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
        Credentials = creds
    }
    

    Hope this helps save someone else some headaches in the future.

提交回复
热议问题