使用 SMTP 客户端发送电子邮件
Sending an Email using SMTP client
我正在尝试使用我的 gmail 凭据通过 SMTP 客户端发送电子邮件。
这是我正在使用的代码
using (var mail = GetMailInfo())
{
using (SmtpClient client = new SmtpClient("smtp.gmail.com", 465))
{
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("**@gmail.com", "Password");
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Send(mail);
}
}
我收到超时错误
The operation has timed out
如果我再试一次,我会收到另一个错误提示
Service not available, closing transmission channel. The server response was: Too many concurrent SMTP connections; please try again later.
我做错了什么?
我终于弄清楚我的代码出了什么问题
更新代码如下
using (var mail = GetMailInfo())
{
using (SmtpClient client = new SmtpClient("smtp.gmail.com", 587)) // port is 587 instead of 465 as mentioned by @jdweng in the comment
{
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false; // this must be before the **Credentials** else this will reset the given credential
client.Credentials = new NetworkCredential("**@gmail.com", "Password");
client.EnableSsl = true;
client.Send(mail);
}
}
如果您使用的是 gmail,请在顶部转到设置并停用双向身份验证和安全性较低的应用程序的访问权限 应该已激活
这些都能找到Here
我正在尝试使用我的 gmail 凭据通过 SMTP 客户端发送电子邮件。 这是我正在使用的代码
using (var mail = GetMailInfo())
{
using (SmtpClient client = new SmtpClient("smtp.gmail.com", 465))
{
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("**@gmail.com", "Password");
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Send(mail);
}
}
我收到超时错误
The operation has timed out
如果我再试一次,我会收到另一个错误提示
Service not available, closing transmission channel. The server response was: Too many concurrent SMTP connections; please try again later.
我做错了什么?
我终于弄清楚我的代码出了什么问题 更新代码如下
using (var mail = GetMailInfo())
{
using (SmtpClient client = new SmtpClient("smtp.gmail.com", 587)) // port is 587 instead of 465 as mentioned by @jdweng in the comment
{
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false; // this must be before the **Credentials** else this will reset the given credential
client.Credentials = new NetworkCredential("**@gmail.com", "Password");
client.EnableSsl = true;
client.Send(mail);
}
}
如果您使用的是 gmail,请在顶部转到设置并停用双向身份验证和安全性较低的应用程序的访问权限 应该已激活
这些都能找到Here