0

I'm trying to send an email from gmail to my Gmail account. is there any other option rather than turn off Less secure option in Gmail account to be able to send email via program?

private void btnSend_Click(object sender, EventArgs e) {
        SmtpClient client = new SmtpClient("smtp@gmail.com", 587);           
        client.EnableSsl = true;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("from@gmail.com", "password");


        MailAddress fromEmail = new MailAddress("from@gmail.com", "From C#");
        MailAddress toAddress = new MailAddress("to@gmail.com", "dogy");

        MailMessage message = new MailMessage()
        {
            From = fromEmail,
            Subject = "just test from c# program",
            Body = "it's coming from c# program"
        };

        message.To.Add(toAddress);

        try {
            client.Send(message);
            MessageBox.Show("Email sent successfully");
        }
        catch (Exception) {
            MessageBox.Show("Failed!");
        }

    }
Talkhak1313
  • 301
  • 2
  • 11
  • What exactly is the problem? Are you getting any exceptions or error codes? – Robert Wolf Jul 04 '21 at 11:12
  • 1
    I don't think smtp@gmail.com is a valid server name. Did you try smtp.gmail.com instead? – rene Jul 04 '21 at 11:19
  • @rene that's right, I changed that to smtp.gmail.com but still failed message box shows up. – Talkhak1313 Jul 04 '21 at 11:47
  • @Robert I get the SMTP server requires a secure connection or the client was not authenticated. the server response was 5.7.0 Authentication required. – Talkhak1313 Jul 04 '21 at 11:48
  • I switched to a different duplicate: read and try all the answers in https://stackoverflow.com/questions/18503333/the-smtp-server-requires-a-secure-connection-or-the-client-was-not-authenticated – rene Jul 04 '21 at 12:06
  • This link shows how to use Gmail to send emails from applications: https://support.google.com/mail/thread/10950702/how-do-i-configure-gmail-to-send-email-from-my-software-specifically-the-apc-software?hl=en basically you need either to have two-step verification enabled on the account or enable the less secure mode, besides it seems that not all the Gmail accounts allow being managed from an external application. – Sergio Prats Jul 04 '21 at 18:35

0 Answers0