使用SMTP客户端发送电子邮件

时间:2020-09-03 09:07:39

标签: c# smtpclient

我正在尝试使用gmail凭据使用SMTP客户端发送电子邮件。 这是我正在使用的代码


BaseClass<T extends MyType> {
  List<T> items;
  void addItemFromMap(Map map) {
    items.add(T.fromMap(map));
  }
}

我收到超时错误

操作已超时

如果我再试一次,我会收到另一个错误消息

服务不可用,正在关闭传输通道。服务器响应为:并发SMTP连接过多;请稍后再试。

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

最后,我弄清楚了我的代码出了什么问题 更新的代码如下

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,请在顶部转到设置并停用2种方式的身份验证,并激活访问安全性较低的应用

所有这些都可以找到Here