[英]Send email using Outlook.com SMTP
我正在尝试使用 Outlook.com Z787C75233B913AA5E45C3F8BE75 发送自动化 email。 但是我得到以下异常:
System.Net.Mail.SmtpException: Failure sending mail.
---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host" Exception while sending email.
我的代码:
public bool SendEmail(MailMessage msg)
{
try
{
SmtpClient smtpClient = new SmtpClient("smtp-mail.outlook.com")
{
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("userAddress", "userPassword"),
Port = 587,
EnableSsl = true,
};
smtpClient.Send(msg);
msg.Dispose();
smtpClient.Dispose();
return true;
}
catch (Exception exp)
{
Console.WriteLine(exp.ToString());
return false;
}
}
我知道这是一个非常古老的问题,我甚至可能无法提供帮助,但是当我尝试使用C#发送电子邮件时,我遇到了类似的问题。
结果我用这个允许我发送电子邮件:
string _sender = "";
string _password = "";
SmtpClient client = new SmtpClient("smtp-mail.outlook.com");
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(_sender, _password);
client.EnableSsl = true;
client.Credentials = credentials;
MailMessage message = new MailMessage(_sender, "recipient of email");
message.Subject = "";
message.Body = "";
client.Send(message);
这可能对你没用了,但万一有人偶然发现这个问题,至少有一个答案,其中有一个工作代码可以作为修复!
我今天在一台旧的 Windows 7 笔记本电脑上遇到了这个确切的问题。 我还注意到,虽然通过我的浏览器可以正常上网,但我无法通过命令提示符从路由器外部 ping 任何东西,因此对 SMTP 的任何尝试都注定会失败。 问题是 IPv6 在网卡上设置为默认值。 如果您遇到此问题,请通过 ping www.google.com进行检查: ping www.google.com
如果您收到“无法访问”,请强制使用 IPv4 ping -4 www.google.com
。 如果您现在收到回复,您可能可以通过您的网卡属性禁用 IPv6 来解决您的问题:
只需取消选中 IPv6 旁边的框并重新启动计算机 go 回到命令提示符并重试直接的“默认”IPv ping: ping www.google.com
。 如果它回复,您的机器现在默认使用 IPv4,所以现在重新尝试您的 SMTP 电子邮件代码。 我使用了本文中的确切代码,它开始工作了。 请注意,您也可以使用相同的逻辑来 ping 任何您的 SMTP 邮件服务器地址,例如。 smtp.live.com 可以帮助您测试无论您的代码如何,您都可以与 SMTP 服务器通信。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.