Gmail-错误使用SMTP客户端发送电子邮件 - 错误net_io_connectionclosed
我们有一个使用DLL发送电子邮件的桌面应用程序。 DLL使用.NET Framework 4.0 SMTP客户端。 在Gmail帐户上启用了2F身份验证并创建了一个应用程序密码之后,我们将不再能够发送电子邮件。应用程序日志中报告的错误是:
“ system.net.mail.smtpexception:错误发送邮件。----> system.io.io.io.ioexception:无法从运输连接中读取数据:net_io_connectionclosed”。
我们是否需要其他任何配置才能使用应用程序密码发送电子邮件? 问候
编辑
这是客户端类。
public Client()
{
_credentials = new NetworkCredential();
_client = new SmtpClient()
{
UseDefaultCredentials = false,
Credentials = _credentials,
DeliveryMethod = SmtpDeliveryMethod.Network
};
Initialize();
RefreshPort();
}
private SmtpClient _client;
private NetworkCredential _credentials;
private int? _port;
public bool Ssl
{
get { return _client.EnableSsl; }
set
{
_client.EnableSsl = value;
RefreshPort();
}
}
public string Host
{
get { return _client.Host; }
set { _client.Host = value; }
}
public int? Port
{
get { return _port ?? _client.Port; }
set
{
_port = value;
RefreshPort();
}
}
public string Username
{
get { return _credentials.UserName; }
set { _credentials.UserName = value; }
}
public string Password
{
get { return _credentials.Password; }
set { _credentials.Password = value; }
}
public TimeSpan Timeout
{
get { return TimeSpan.FromMilliseconds(_client.Timeout); }
set { _client.Timeout = (int)value.TotalMilliseconds; }
}
public string DisplayName { get; set; }
private void RefreshPort()
{
_client.Port = _port ?? (Ssl ? 587 : 25);
}
这是发送电子邮件的方法
public void Send(Message message)
{
using (MailMessage mail = new MailMessage()
{
From = new MailAddress(Username, DisplayName),
Subject = message.Subject,
Body = message.Body,
IsBodyHtml = message.Html
})
{
message.To?.ForEach(to => mail.To.Add(new MailAddress(to)));
message.CC?.ForEach(cc => mail.CC.Add(new MailAddress(cc)));
message.BCC?.ForEach(bcc => mail.Bcc.Add(new MailAddress(bcc)));
message.Attachments?.ForEach(attachment => mail.Attachments.Add(new Attachment(attachment)));
if (message.Encoding != null)
{
mail.SubjectEncoding = message.Encoding;
mail.BodyEncoding = message.Encoding;
}
_client.Send(mail);
}
}
We have a desktop application that uses a dll to send email. The dll uses .net framework 4.0 smtp client.
After enabled 2F authentication on gmail account and created an app password, we are no longer able to send email. The error reported in application's log is:
"System.Net.Mail.SmtpException: Error sending mail. ---> System.IO.IOException: Unable to read data from transport connection: net_io_connectionclosed".
Do we need any other configurations to allow sending email using app password?
Regards
EDIT
This is the Client class.
public Client()
{
_credentials = new NetworkCredential();
_client = new SmtpClient()
{
UseDefaultCredentials = false,
Credentials = _credentials,
DeliveryMethod = SmtpDeliveryMethod.Network
};
Initialize();
RefreshPort();
}
private SmtpClient _client;
private NetworkCredential _credentials;
private int? _port;
public bool Ssl
{
get { return _client.EnableSsl; }
set
{
_client.EnableSsl = value;
RefreshPort();
}
}
public string Host
{
get { return _client.Host; }
set { _client.Host = value; }
}
public int? Port
{
get { return _port ?? _client.Port; }
set
{
_port = value;
RefreshPort();
}
}
public string Username
{
get { return _credentials.UserName; }
set { _credentials.UserName = value; }
}
public string Password
{
get { return _credentials.Password; }
set { _credentials.Password = value; }
}
public TimeSpan Timeout
{
get { return TimeSpan.FromMilliseconds(_client.Timeout); }
set { _client.Timeout = (int)value.TotalMilliseconds; }
}
public string DisplayName { get; set; }
private void RefreshPort()
{
_client.Port = _port ?? (Ssl ? 587 : 25);
}
And this is the method to send the email
public void Send(Message message)
{
using (MailMessage mail = new MailMessage()
{
From = new MailAddress(Username, DisplayName),
Subject = message.Subject,
Body = message.Body,
IsBodyHtml = message.Html
})
{
message.To?.ForEach(to => mail.To.Add(new MailAddress(to)));
message.CC?.ForEach(cc => mail.CC.Add(new MailAddress(cc)));
message.BCC?.ForEach(bcc => mail.Bcc.Add(new MailAddress(bcc)));
message.Attachments?.ForEach(attachment => mail.Attachments.Add(new Attachment(attachment)));
if (message.Encoding != null)
{
mail.SubjectEncoding = message.Encoding;
mail.BodyEncoding = message.Encoding;
}
_client.Send(mail);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您目前正在使用要连接到的Gmail帐户的实际密码。最好的办法是尝试创建应用程序密码并使用它。
登录App passwords
更新
我刚刚测试了它,这似乎是对此进行了测试工作正常。
另一个选项是 xoauth2
If you are currently using the actual password for the gmail account you are connecting to. The the best thing to do would be to try creating an apps password and using that.
Sign in with App Passwords
update
I just tested it and this appears to work fine.
Another option would be xoauth2