No connection could be made because the target machine actively refused it ipadd:port at system.Net.sockets.Socket.doConnect

Posted on 2010-09-09
Last Modified: 2012-05-10

I am writing code to send  an email whenever my application has an unhandled error.

With this same code i could get it runnign on a gmail smtp server.

However, when i replaced these values wit hvalues for my smtp host, i am gettin the following error. Why am i getting this error?

I am getting the following error.System.Net.Mail.smtpException:Failure sending Mail.----->System.Net.WebException:Unable to connect to remote server---->System.Net.Sockets.socketException:No connection could be made because the target machine actively refused it ipadd:port at system.Net.sockets.Socket.doConnect

Here is my code.
Private Sub EmailTest(ByVal errorMsg As String)
    Dim SmtpServer As New SmtpClient()
    SmtpServer.Credentials = New Net.NetworkCredential("", "test123")
    SmtpServer.Port = 22
    SmtpServer.Host = ""
    SmtpServer.EnableSsl = True

    Dim mail As New MailMessage()

      mail.From = New MailAddress("", "Web Developers", System.Text.Encoding.UTF8)
      mail.Subject = "Error Messages in Application"
      mail.Body = errorMsg
    Catch ex As Exception
    End Try

  End Sub

Open in new window

Question by:shahjagat
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3

Expert Comment

ID: 33637833
look like your firewall is blocking the port your trying to connect to
LVL 11

Expert Comment

ID: 33638080
This could be a firewall issue, it could also be a port issue. If the server is not setup for anonymous relay, it may be expecting  connections on an SSL port. Do you have access to the server configuration, and what mail system is it running?

Author Comment

ID: 33638229
Hi sdrouins and ladarling,

I am getting this issue now.
System.Security.Authentication.AuthenticationException:The Remote Certificate is Invalid According tothe validation procedure.

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?


Author Comment

ID: 33638421

The server is already set up for anonymous relay i guess as we have our outlook conencted to it.

what mail system is it running? - I am not sure what you mean by this.
LVL 11

Accepted Solution

ladarling earned 500 total points
ID: 33639661
If you are setup for anonymous relay, you dont need to use the credentials -or- SSL, and this is normally done on port 25 for exchange.
If you need a secure socket, exchange default is Port 443, and your client machine must have the SSL certificate for the server in the trusted certificates chain.
Below is a sample function that we user here.... Replace MyExchangeServer with your actual machine name or its IP address and fill in your own Email address in the from field and it should work.

    Public Shared Function SendMail(ByVal toRecipients() As String, ByVal messageSubject As String, ByVal messageText As String, Optional ByVal ccRecipients() As String = Nothing, Optional ByVal fileAttachments() As System.IO.FileInfo = Nothing) As Boolean

        Dim client As New SmtpClient("MyExchangeServer")
        Dim msg As New MailMessage
        With msg
            For Each s As String In toRecipients
            For Each s As String In ccRecipients
            .Subject = messageSubject
            .Body = messageText
            .From = New MailAddress("")
            For Each a As IO.FileInfo In fileAttachments
                .Attachments.Add(New Net.Mail.Attachment(a.FullName))
        End With
        Catch ex As Exception
            Return False
        End Try
        Return True

    End Function

Open in new window

LVL 11

Expert Comment

ID: 33640061
If you are -not- interacting with a Microsoft Exchange email server, you need to verify the SSL and Relay ports for your provider and investigate what kind of authentication is necessary (if it requires SSL, for example). If this is a third party server, you will most likely have to authenticate. Try using your code again, but setting
SmtpServer.UseDefaultCredentials = False

Author Comment

ID: 33640464
I am working on that - Have to contact them.

Author Comment

ID: 33640545
I am not using a microsft Exchange email server. We are using a 3rd party server.

Author Comment

ID: 33641965
Hi ladarling,

when i changed  the following from true to false, i am getting the mails.
SmtpServer.EnableSsl = False


Author Comment

ID: 33663705
Hi ,  I do not see Mark As Answer buttons.

Please award  points to ladarling - Comment ID: 33639661


Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question