C# 无法连接到smtp远程服务器
我无法连接到远程SMTP服务器。防火墙未阻止端口587和25。我曾尝试使用telnet,但它不起作用C# 无法连接到smtp远程服务器,c#,smtp,C#,Smtp,我无法连接到远程SMTP服务器。防火墙未阻止端口587和25。我曾尝试使用telnet,但它不起作用 try { System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("test04@outlook.com"); System.Net.Mail.MailA
try {
System.Net.Mail.MailAddress from = new
System.Net.Mail.MailAddress("test04@outlook.com");
System.Net.Mail.MailAddress to = new
System.Net.Mail.MailAddress("test@gmail.com");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.IsBodyHtml = false;
message.Subject = "Hello";
message.Body = "My Name is Simple Blog";
using (System.Net.Mail.SmtpClient client = new
System.Net.Mail.SmtpClient("smtp.office365.com",587))
{
client.UseDefaultCredentials = false;
client.Credentials =new NetworkCredential("username", "password");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(message);
Console.WriteLine("Sent");
}
}
catch(SmtpException ex)
{
if(ex.InnerException !=null)
{
Console.WriteLine(ex.InnerException);
}
}
错误:
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 52.98.16.226:587
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
如果我没弄错你的问题,你想用你的C#程序从Outlook.com帐户发送一封电子邮件 要从outlook发送带有smtp的电子邮件,您必须确保遵守。它谈到默认情况下禁用smtp,您必须为您的帐户启用它 另外,如果您已经配置了MFA(比如令牌或文本),那么您就不走运了,因为它不会接受您的密码。在这种情况下,您应该创建一个特殊的“应用程序密码”,如前所述 只要遵循这些指南(正确的主机和端口),代码看起来就很好。你应该没事的
不过,我建议对应用程序使用特殊的smtp提供程序。例如sendgrid,如果您不发送很多邮件,他们有一个不错的“免费”计划。您是否能够使用电子邮件客户端从同一台机器连接到该邮件服务器?您是否在公司网络中?代码在你的家用电脑上工作吗?你不能从公司网络内部访问gmail。防火墙正在将端口25和587的端口转发到用于outlook的内部代理服务器。以前工作正常。。我不知道为什么会出现错误,因为在连接到另一个邮件服务器时显示了相同的错误。。像smtp.gmail.com
之前工作正常
…在什么之前?确定发生的更改,然后您可能有机会解决该问题。但错误消息的简单含义是,您的代码甚至无法连接到该服务器,更不用说尝试发送电子邮件了<代码>防火墙没有阻止587和25端口…嗯,有什么东西阻止了它。你需要弄清楚那是什么。我们无法真正帮助您,因为我们对您的网络配置一无所知,您也没有回答Llama或jdweng的问题。