Error on sending email using gmail. The SMTP serve

2019-08-22 04:17发布

Here is my code.

var smtp = new SmtpClient("smtp.gmail.com", 587)
        {

            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential("sender@gmail.com","123")
        };
        try
        {
            smtp.Send("sender@gmail.com", "receiver@gmail.com", "test", "testbody");
        }
        catch (SmtpException e)
        {
            ltrInfo.Text=e.Message;
        }

When I debug the code, I get the error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required" Please can you tell me what's the wrong with the code above?

1条回答
来,给爷笑一个
2楼-- · 2019-08-22 05:13

Everything looks ok.

Here are 2 possible solutions

Get to your Gmail account's security settings and set permissions for "Less secure apps" to Enabled.

Check if there is not a "Suspicious login attempt" on the top of the page, if is there click to authorizing the access

take a look on this Tell me if works.

查看更多
登录 后发表回答