无法连接到远程服务器 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19543273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:14:50  来源:igfitidea点击:

Unable to connect to the remote server C#

c#smtpclient

提问by user2901683

I made this console application which scans over a folder for files and sends them via email as attachments.It works fine in my local machine. But when I try to run it remotely on another machine(testing server), it gives me an exception.

我制作了这个控制台应用程序,它扫描文件夹中的文件并通过电子邮件将它们作为附件发送。它在我的本地机器上运行良好。但是当我尝试在另一台机器(测试服务器)上远程运行它时,它给了我一个例外。

This is exception that I see.

这是我看到的例外。

 innerException  {System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions *******:25
       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)} System.Exception {System.Net.WebException}

I'm not sure why would this work on on my local machine and not on the server. Is that because an outlook account is not set up at the server ?

我不确定为什么这会在我的本地机器上而不是在服务器上工作。那是因为服务器上没有设置 Outlook 帐户吗?

This is the mailing code in my application -

这是我的应用程序中的邮件代码 -

public static void SendMailMessage(string file) 
{
    const string subject = "TESTING";
    const string body = "";

    string from = ConfigurationManager.AppSettings["from"];        
    string to = ConfigurationManager.AppSettings["to"];
    var message = new MailMessage(from, to, subject, body);
    message.Attachments.Add(new Attachment(file));
    var client = new SmtpClient("smtp.mail***.com")
    {
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = true

    };

    try
    {
        client.Send(message);
        Console.WriteLine("Email Successfully sent!");

    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

回答by user2901683

Problem is that you are using default credentials on remote which is denied by remote server, as there no way to access default credential in such environment. Try this it will work.

问题是您正在远程服务器上使用默认凭据,因为在这种环境中无法访问默认凭据。试试这个它会起作用。

 var client = new SmtpClient("smtp.mail***.com",port)
 {  
    NetworkCredentials = new NetworkCredentials("username","password")        
 };
 client.Send();

回答by Jorgesys

* Remember, we need to add network credentials:

* 请记住,我们需要添加网络凭据:

 SmtpClient client = new SmtpClient();
        client.Credentials = new    System.Net.NetworkCredential("[email protected]", "patito12");
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;
        client.Send(mail);

sometimes the error : "Unable to connect to the remote server" is raised because the server port 587is blocked.

有时会出现错误:“无法连接到远程服务器”,因为服务器端口587被阻止。