发送邮件失败:{“无法解析远程名称:'smtp.live.com'”}

时间:2017-03-27 12:49:43

标签: c# visual-studio-2010 smtpclient mailmessage system.net.mail

我在尝试发送简单邮件时,过去2小时内连续出现“无法解析远程名称:'smtp.live.com'”异常。 这是我的代码:

            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("someemail@outlook.com", "Some Name");
            mailMessage.To.Add(new MailAddress("someemail@domain.com"));
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = "<b>This is a check email</b>";
            mailMessage.Subject = "Test subject";

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Port = 587; //also tried 25 and 465
            smtpClient.Host = "smtp.live.com"; //also tried smtp-mail.outlook.com
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false; //also tried removing the credentials and setting this to true
            smtpClient.Credentials = new NetworkCredential("someemail@outlook.com", "its password");

            smtpClient.Send(mailMessage);

            Console.WriteLine("Success");

出于好奇,我尝试将smtp.gmail.com添加到主机并输入我的gmail凭据,但这也无效。这是跟踪:

System.Net.Mail.SmtpException was caught
  Message=Failure sending mail.
  Source=System
  StackTrace:
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at ConsoleApplication1.Program.Main(String[] args) in C:\Users\italha\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 95
  InnerException: System.Net.WebException
       Message=The remote name could not be resolved: 'smtp.live.com'
       Source=System
       StackTrace:
            at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
            at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, 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)
       InnerException: 

以下是nslookup向我展示smtp.live.com的内容: enter image description here

我的网络连接使用代理地址和端口8080,这是问题吗?请为我的具体问题提供解决方案,我已经尝试过在互联网上提供的所有通用解决方案。

2 个答案:

答案 0 :(得分:0)

您的网络设置似乎存在问题,或者防火墙阻止了邮件服务器。代码似乎很好。

尝试使用跟踪ping主机名,看看是否有效

答案 1 :(得分:-3)

正确检查互联网连接,然后重试。 这对我有用。