无法在 Windows 表单中使用 SMTP 发送电子邮件
Cannot send email with SMTP in Windows Forms
因此,我正在处理 windows 表单并尝试使用 smtp 发送电子邮件。
这是一个代码:
MailMessage mail = new MailMessage(from, to, subject, text);
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 465;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(from, password);
try
{
client.Send(mail);
MessageBox.Show("Mesage has benn sant");
}
catch (Exception ex)
{
MessageBox.Show("Failure while sending message");
MessageBox.Show(ex.Message);
}
当我 运行 此代码时,出现以下错误:
"Failure sending email".
当我将端口更改为 587 时,我得到以下信息:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication REquired".
那么为什么我无法发送电子邮件?谁能给我解释一下?
我已将端口更改为 25 并从 hotmail 发送邮件(我已将 "smtp.gmail.com" 替换为 "smtp.live.com"),而不是从 gmail。它有效。 gmail 好像有问题。
先把端口改成25
此外,client.Credentials
的参数应该是您用于登录的用户名和密码。例如...
client.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "mypassword");
通过添加以下三行并更改 gmail 帐户设置,我能够使您的代码正常工作:
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential (from, password);
事实证明,除了上述更改之外,您还需要更新 google 帐户设置并允许安全性较低的应用。
我是在这个帖子上了解到的:Sending email in .NET through Gmail
希望对您有所帮助。
我遇到了同样的问题。能够通过禁用此安全设置来修复它:
转到此 link 并打开 "access for less secure apps"
因此,我正在处理 windows 表单并尝试使用 smtp 发送电子邮件。 这是一个代码:
MailMessage mail = new MailMessage(from, to, subject, text);
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 465;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(from, password);
try
{
client.Send(mail);
MessageBox.Show("Mesage has benn sant");
}
catch (Exception ex)
{
MessageBox.Show("Failure while sending message");
MessageBox.Show(ex.Message);
}
当我 运行 此代码时,出现以下错误:
"Failure sending email".
当我将端口更改为 587 时,我得到以下信息:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication REquired".
那么为什么我无法发送电子邮件?谁能给我解释一下?
我已将端口更改为 25 并从 hotmail 发送邮件(我已将 "smtp.gmail.com" 替换为 "smtp.live.com"),而不是从 gmail。它有效。 gmail 好像有问题。
先把端口改成25
此外,client.Credentials
的参数应该是您用于登录的用户名和密码。例如...
client.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "mypassword");
通过添加以下三行并更改 gmail 帐户设置,我能够使您的代码正常工作:
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential (from, password);
事实证明,除了上述更改之外,您还需要更新 google 帐户设置并允许安全性较低的应用。
我是在这个帖子上了解到的:Sending email in .NET through Gmail
希望对您有所帮助。
我遇到了同样的问题。能够通过禁用此安全设置来修复它:
转到此 link 并打开 "access for less secure apps"