无法连接到 smtp 远程服务器

时间:2021-04-16 09:37:01

标签: c# smtp

我无法连接到远程 SMTP 服务器。防火墙没有阻止端口 587 和 25。我尝试使用 telnet 但它不起作用。

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)    
     

          

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您想使用您的 C# 程序从某个 Outlook.com 帐户发送一封电子邮件。

要从 Outlook 发送带有 smtp 的电子邮件,您确实必须确保遵循 this manual。它谈到默认情况下禁用 smtp,您必须为您的帐户启用它。

此外,如果您配置了 MFA(因此是令牌或文本),那么您就不走运了,因为它不会接受您的密码。在这种情况下,您应该按照 here 的说明创建一个特殊的“应用密码”。

您的代码看起来不错,只要您遵循了这些指南(正确的主机和端口)。你应该没事。

不过,我建议为应用程序使用特殊的 smtp 提供程序。例如sendgrid,如果你不发送很多邮件,他们有一个不错的“免费”计划。