Avatar ·

通过Gmail SMTP在.NET中发送邮件:解决密码和设置问题

大家好!我是C#新手,正在尝试在我的应用程序中通过Gmail发送邮件。我使用标准的SmtpClient。写了如下代码: ```csharp using System.Net; using System.Net.Mail; var fromAddress = new MailAddress("myemail@gmail.com", "My Name"); var toAddress = new MailAddress("recipient@gmail.com", "Recipient"); const string fromPassword = "mypassword123"; const string subject = "Test"; const string body = "Hello from C#!"; var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body }) { smtp.Send(message); } ``` 但执行时收到错误:"5.5.1 Authentication Required" 或 "5.7.0 Username and Password not accepted"。我