Avatar ·

Sending email via Gmail SMTP in .NET: solving password and settings issues

Hello everyone! I'm a beginner in C# and trying to implement sending emails via Gmail in my application. I'm using the standard SmtpClient. I wrote this code:\n\n```csharp\nusing System.Net;\nusing System.Net.Mail;\n\nvar fromAddress = new MailAddress("myemail@gmail.com", "My Name");\nvar toAddress = new MailAddress("recipient@gmail.com", "Recipient");\nconst string fromPassword = "mypassword123";\nconst string subject = "Test";\nconst string body = "Hello from C#!";\n\nvar smtp = new SmtpClient\n{\n Host = "smtp.gmail.com",\n Port = 587,\n EnableSsl = true,\n DeliveryMethod = SmtpDeliveryMethod.Network,\n UseDefaultCredentials = false,\n Credentials = new NetworkCredential(fromAddress.Address, fromPassword)\n};\n\nusing (var message = new MailMessage(fromAddress, toAddress)\n{