I am trying to send an email using C# using the following function:
public static void SendEmail(string fromAddress, string password)
{
SmtpClient email = new SmtpClient
{
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
EnableSsl = true,
Host = "smtp.gmail.com",
Port = 587,
Credentials = new NetworkCredential(fromAddress, password)
};
string subject = "Mail subject";
string body = "Body";
try
{
Console.WriteLine("Sending email...");
email.Send(fromAddress, GetToAddress(), subject, body);
} catch (SmtpException e)
{
Console.WriteLine(e.Message);
}
}
I see that in order for it to work, I need to enable less secure apps on my gmail account. However, according to google "To help keep your account secure, from May 30, 2022, Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password. ". Is there any other way to send a mail?
CodePudding user response:
you can enable two-factor authentication on the google account and then create an apps password
This will generate 16-character "App password". Then take the exact same code you used before and replace the standard user Gmail password with the apps password.
ex:
SendEmail("[email protected]", "iodwsurnuyjfsxdc") // here iodwsurnuyjfsxdc means app password and [email protected] means app pass generated account email