[英]Sending an Email using SMTP client
I am trying to send an email using SMTP client using my gmail credential.我正在尝试使用我的 gmail 凭据使用 SMTP 客户端发送电子邮件。 This is the code am using
这是我使用的代码
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);
}
}
I receive a time out error我收到超时错误
The operation has timed out
操作已超时
If I try again I get another error saying如果我再试一次,我会收到另一个错误提示
Service not available, closing transmission channel.
服务不可用,关闭传输通道。 The server response was: Too many concurrent SMTP connections;
服务器响应为:并发 SMTP 连接过多; please try again later.
请稍后再试。
What am I doing wrong?我究竟做错了什么?
At last I figure it out what was wrong with my code Updated code is below最后我弄清楚我的代码有什么问题更新的代码如下
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);
}
}
On the top if you are using gmail go to settings and deactivate 2 way authentification and Access for less secure apps should be Activated在顶部,如果您使用的是 gmail,请转到设置并停用 2 路身份验证和访问不太安全的应用程序应该被激活
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.