1

I've have founded a simple function to send emails using a personal google acount. This is the code:

public void SendMail()
{
        // Prepare mail
        MailMessage msg = new MailMessage();
        SmtpClient smtp = new SmtpClient();
        msg.From = new MailAddress("example@gmail.com");
        msg.To.Add(new MailAddress("examplereceiver@gmail.com"));
        msg.Subject = "Example subject";
        msg.IsBodyHtml = true; //to make message body as html  
        msg.Body = "Hi, this is the mail content";

        // Prepare client SMTP
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com"; //for gmail host  
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new NetworkCredential("example@gmail.com", "passwordExample");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

        // Send mail
        smtp.Send(msg);
 }

The first time I tried this function out, it threw the error 'The SMTP server requires a secure connection or the client was not authenticated', which I could work around by enabeling the 'less secure apps' option on my Google account made for testing.

Recently I got stuck with the same error and I'm having trouble finding solutions since Google decided to disable the 'less secure apps' option (from May 30 2022) Does anyone know a way of configuring the function to work again? or is there a security configuration Google asks for that I'm missing out?

Thanks for reading, any help apreaciated.

0 Answers0