Gmail 发送错误 (C#)
Gmail sending error (C#)
我正在尝试通过 C# 发送电子邮件,但我一直收到此错误:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at...
可以在此处找到堆栈跟踪:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at Repetrel.View.EmailForm.btnSend_Click(Object sender, EventArgs e) in c:15 Hyrel Projects\Repetrel\src\Repetrel\View\EmailForm.cs:line 238
这是我的代码:
NetworkCredential loginInfo = new NetworkCredential(Properties.Settings.Default.MyEmailAddress, Properties.Settings.Default.MyEmailPassword);
//System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(Properties.Settings.Default.SMTPAddress);
////Trace.WriteLine("smtp = " + Properties.Settings.Default.SMTPAddress);
//smtpClient.Port = 587;
//smtpClient.EnableSsl = true;
//smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
////smtpClient.UseDefaultCredentials = false;
//smtpClient.Credentials = loginInfo;
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = true,
Credentials = loginInfo,
Timeout = 20000
};
//smtpClient.SendAsync(message, "test");
smtpClient.Send(message);
//Output to log
OutputToLog(message);
//Close Window
Close();
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
MessageBox.Show("An error occurred. Please see the trace log for more information.");
}
我已经尝试了之前帖子中提到的所有方法(有很多),但似乎没有任何效果。我尝试过使用和不使用默认凭据并将标志设置为 false/true。似乎没有任何效果。谁能给我指出正确的方向?
提前致谢!
也许 UseDefaultCredentials = false,
而不是?
编辑: 这似乎类似于一个较早的 Whosebug 问题:SmtpClient with Gmail
显然 GMAIL 阻止我的电子邮件地址使用不太安全的设备。您可以在以下位置关闭阻止:https://www.google.com/settings/security/lesssecureapps.
我正在尝试通过 C# 发送电子邮件,但我一直收到此错误:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at...
可以在此处找到堆栈跟踪:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at Repetrel.View.EmailForm.btnSend_Click(Object sender, EventArgs e) in c:15 Hyrel Projects\Repetrel\src\Repetrel\View\EmailForm.cs:line 238
这是我的代码:
NetworkCredential loginInfo = new NetworkCredential(Properties.Settings.Default.MyEmailAddress, Properties.Settings.Default.MyEmailPassword);
//System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(Properties.Settings.Default.SMTPAddress);
////Trace.WriteLine("smtp = " + Properties.Settings.Default.SMTPAddress);
//smtpClient.Port = 587;
//smtpClient.EnableSsl = true;
//smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
////smtpClient.UseDefaultCredentials = false;
//smtpClient.Credentials = loginInfo;
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = true,
Credentials = loginInfo,
Timeout = 20000
};
//smtpClient.SendAsync(message, "test");
smtpClient.Send(message);
//Output to log
OutputToLog(message);
//Close Window
Close();
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
MessageBox.Show("An error occurred. Please see the trace log for more information.");
}
我已经尝试了之前帖子中提到的所有方法(有很多),但似乎没有任何效果。我尝试过使用和不使用默认凭据并将标志设置为 false/true。似乎没有任何效果。谁能给我指出正确的方向?
提前致谢!
也许 UseDefaultCredentials = false,
而不是?
编辑: 这似乎类似于一个较早的 Whosebug 问题:SmtpClient with Gmail
显然 GMAIL 阻止我的电子邮件地址使用不太安全的设备。您可以在以下位置关闭阻止:https://www.google.com/settings/security/lesssecureapps.