简体   繁体   中英

Unable to connect to the remote server C#

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 ?

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);
    }
}

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();

* Remember, we need to add network credentials:

 SmtpClient client = new SmtpClient();
        client.Credentials = new    System.Net.NetworkCredential("jorgesys@gmail.com", "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 587 is blocked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM