c# - 使用 SMTP 客户端发送电子邮件
问题描述
我正在尝试使用我的 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);
}
}
我收到超时错误
操作已超时
如果我再试一次,我会收到另一个错误消息
服务不可用,正在关闭传输通道。服务器响应为:并发 SMTP 连接过多;请稍后再试。
我究竟做错了什么?
解决方案
最后我弄清楚我的代码出了什么问题更新的代码如下
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 向身份验证,并应激活安全性较低的应用程序的访问权限
所有这些都可以在这里找到
推荐阅读
- salesforce - Salesforce 触发器/出站 api 调用似乎不适用于批量捐赠
- python - Python BS4 find_all 将标签内的文本替换为
- c# - c# Xamarin Android WebView 适合横屏
- tfs - 授予用户在 TFS 2015 中分配任务和错误的权限
- python - Graphics.py,我想在鼠标点击时创建三个单独的对象
- google-bigquery - 如何在 BigQuery 中可靠有效地散列一行?
- mysql - 如果有多个用户并且他们都有多个条目,那么将图像存储到数据库的最佳方法是什么?
- linux - 在 debian 上安装 docker
- r - gsub正则表达式方法在R列中提取多个带引号的字符串
- c# - HttpWebRequest 根本没有收到响应