0

I am trying to send email through gmail server but is giving exception.The exception thrown is --> SMTP server requires a secure connection or the client was not authenticated.The server response was 5.7.0.Authentication required.

        try
        {
            MailMessage mail = new MailMessage();

            mail.From = new MailAddress("account1@gmail.com");
            mail.To.Add("account2@gmail.com");
            mail.Subject = "Hello World";
            mail.Body = "<h1>Hello</h1>";
            mail.IsBodyHtml = true;
            //    mail.Attachments.Add(new Attachment("C:\\file.zip"));

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

            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential("account1@gmail.com", "password");
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }
nashco
  • 11
  • 5
  • 2
    what is your problem exactly and what is your question? – Mong Zhu Nov 01 '22 at 10:59
  • My problem is clear .i am not able to send mail using the above code and i have mentioned the exception it is throwing. – nashco Nov 01 '22 at 11:05
  • Post the exception that it throws. – Yuri Melo Nov 01 '22 at 11:05
  • The exception thrown is --> SMTP server requires a secure connection or the client was not authenticated.The server response was 5.7.0.Authentication required. – nashco Nov 01 '22 at 11:10
  • Your code looks correct for a gmail connection, though it is possible google is trying to raise additional authentication because it does not recognize your locatino, e.g. https://support.google.com/mail/thread/146949535/the-server-response-was-5-7-0-authentication-required?hl=en – possum Nov 01 '22 at 11:11
  • 3
    Is password an APP Password here or the personal users password for that account. If i recall correctly GMail does not allow simple Credentials to be used for authentication here. https://support.google.com/accounts/answer/6010255?hl=en – Ralf Nov 01 '22 at 11:13
  • You need to use DefaultCredentials. To connect you have to setup a mail account for the user on machine. – jdweng Nov 01 '22 at 11:31
  • Does this answer your question? [Sending email through Gmail SMTP server with C#](https://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) – BurnsBA Nov 01 '22 at 13:37
  • Instead of using personal account password ,i used an app password and it worked. – nashco Nov 02 '22 at 00:57

0 Answers0