Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

c# - Send email using gmail. Error: The server response was: 5.5.1 Authentication Required

Sorry, I saw a lot of similar post regarding this matter but never found any solution to my problem, so I decided to post it.

I am using ASP.NET c# to send email programmatically using gmail with the following code.

 string EmailAddress = senderemail;
    MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress);
    mailMessage.Subject = "This is a test email";
    mailMessage.Body = "This is a test email. Please reply if you receive it.";

    SmtpClient smtpClient = new SmtpClient();
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.EnableSsl = true;
    smtpClient.Host = "smtp.gmail.com";
    smtpClient.Port = 587;

    smtpClient.Credentials = new System.Net.NetworkCredential()
    {
        UserName = EmailAddress,
        Password = senderpassword
    };
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Send(mailMessage);

I received this error like everybody else

Error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at.

Already did below 2 action from GMAIL.

-less secure app : turn on

-2 step verification : off

I don't care whether is this gmail account safe or what. I don't need any security for this account. What else should i do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

sign up Gmail and go to https://www.google.com/settings/security/lesssecureapps where you can see settings. Access for less secure apps

Turn off (default) ==> Turn On

after add this code,

  MailMessage mail = new MailMessage("[email protected]", "[email protected]");
                mail.Subject = "TestEmailImportant";
                mail.Body = "This mail is to test if this program is working";

                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);

                smtpClient.Credentials = new System.Net.NetworkCredential()
                {
                    UserName = "[email protected]",
                    Password = "YYYYYYYY"
                };

                smtpClient.EnableSsl = true;
                System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s,
                        System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                        System.Security.Cryptography.X509Certificates.X509Chain chain,
                        System.Net.Security.SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                };

                smtpClient.Send(mail);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...