'"Unable to connect to the remote server" Error

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

this is my code:

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("[email protected]", "web");

         message.From = fromAddress;
         message.To.Add("[email protected]"); 
         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"

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?



Solution 1:[1]

Instead of:

smtpClient.Credentials = new System.Net.NetworkCredential("web", "test1234", "myDomain");

try:

smtpClient.Credentials = new System.Net.NetworkCredential("emailaddress", "password");

Here is an example of a task I use to send email:

public Task SendEmail(string email, string subject, string body)
 {
 try
 {
 var mailClient = new SmtpClient
 {
 Host = "mail.mydomain.com",
 Credentials = new NetworkCredential("[email protected]", "mypasword"),
 Port = myport,
 DeliveryMethod = SmtpDeliveryMethod.Network
 };

 var msg = new MailMessage("from email", email, subject,body) {IsBodyHtml = true};
 mailClient.Send(msg);

 return Task.FromResult(0);
 }
 catch (Exception ex)
 {
 return null;
 }
 }

Solution 2:[2]

I have same problem. Console applications uses system's proxy but web applications don't... You may miss setting proxy attributes of your client service object. My problem is solved by this way. Wish it will be helpful.

Solution 3:[3]

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.

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)

Resolution: Set the proxy in Web config.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mbithy Mbithy
Solution 2 Mumin Ka
Solution 3