“无法连接到远程服务器"错误 [英] "Unable to connect to the remote server" Error

查看:90
本文介绍了“无法连接到远程服务器"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的网站发送电子邮件.

I'm trying to send an email from my website.

这是我的代码:

public string SendEmail()
    {
    string sMessage;
    SmtpClient smtpClient = new SmtpClient("mail.test.com",25);
    MailMessage message = new MailMessage();
    try
        {
         smtpClient.Credentials = new System.Net.NetworkCredential("web", "test1234", "myDomain");
         MailAddress fromAddress = new MailAddress("web@test.com", "web");

         message.From = fromAddress;
         message.To.Add("test@gmail.com"); 
         message.Subject = "test";
         message.Body = "TEST";
         message.IsBodyHtml = true;
         smtpClient.EnableSsl = false; 
         smtpClient.Send(message);
         sMessage = "Email sent.";
        }
        catch (Exception ex)
        {
            sMessage = "Coudn't send the message!\n " + ex.Message;
        }
        return "";
    }

但是我遇到了以下错误:

But I'm getting the following error:

无法连接到远程服务器"

"Unable to connect to the remote server"

数据(主机名,端口,凭据)是正确的,我在几个SMTP检查器中进行了检查(例如,这个),效果很好.

The data (host name, port, credentials) is correct, I checked it in a few SMTP checkers (for ex this one) and it worked fine.

我尝试了所有在网上找到的内容:

I tried everything I found online:

smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

不同的端口,但无济于事,我不断收到错误消息.

And different ports but nothing helped, I keep getting the error.

我在做什么错了?我想念什么吗?

What am I doing something wrong? Am I missing something?

推荐答案

使用类似的模式,我的其余客户端正在调用服务API,该服务在调试时成功调用,但无法处理已发布的代码.错误是:无法连接到远程服务器.

With similar pattern, my rest client is calling the service API, the service called successfully when debugging, but not working on the published code. Error was: Unable to connect to the remote server.

内部异常:System.Net.Sockets.SocketException(0x80004005):无法建立连接,因为目标计算机主动拒绝了它服务IP:443在System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)在System.Net.ServicePoint.ConnectSocketInternal处(Boolean connectFailure,Socket s4,Socket s6,Socket&套接字,IPAddress&地址,ConnectSocketState状态,IAsyncResult asyncResult,Exception&Exception)

Inner Exception: System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it serviceIP:443 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)

解决方案::在Web配置中设置代理.

Resolution: Set the proxy in Web config.

<system.net>
    <defaultProxy useDefaultCredentials="true">
      <proxy proxyaddress="http://proxy_ip:portno/" usesystemdefault="True"/>
    </defaultProxy>
</system.net>

这篇关于“无法连接到远程服务器"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆