Solved

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
12
968 Views
Last Modified: 2012-05-10
Hi,

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("me@tremail.com", "test123")

    SmtpServer.Port = 22

    SmtpServer.Host = "smtp.myhost.com"

    SmtpServer.EnableSsl = True



    Dim mail As New MailMessage()



    Try

      mail.From = New MailAddress("me@tremail.com", "Web Developers", System.Text.Encoding.UTF8)

      mail.To.Add("target@tremail.com")

      mail.Subject = "Error Messages in Application"

      mail.Body = errorMsg

      

      SmtpServer.Send(mail)

    Catch ex As Exception

      MsgBox(ex.ToString())

    End Try



  End Sub

Open in new window

0
Comment
Question by:shahjagat
  • 6
  • 3
12 Comments
 
LVL 4

Expert Comment

by:sdrouins
Comment Utility
look like your firewall is blocking the port your trying to connect to
0
 
LVL 11

Expert Comment

by:ladarling
Comment Utility
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?
0
 

Author Comment

by:shahjagat
Comment Utility
Hi sdrouins and ladarling,

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

Thanks
0
 

Author Comment

by:shahjagat
Comment Utility
ladarling:

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.
Thanks
0
 
LVL 11

Accepted Solution

by:
ladarling earned 500 total points
Comment Utility
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

                .To.Add(s)

            Next

            For Each s As String In ccRecipients

                .CC.Add(s)

            Next

            .Subject = messageSubject

            .Body = messageText

            .From = New MailAddress("me@mydomain.com")

            For Each a As IO.FileInfo In fileAttachments

                .Attachments.Add(New Net.Mail.Attachment(a.FullName))

            Next

        End With

        Try

            client.Send(msg)

        Catch ex As Exception

            MsgBox(ex.Message)

            Return False

        End Try

        Return True



    End Function

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:ladarling
Comment Utility
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
 
0
 

Author Comment

by:shahjagat
Comment Utility
I am working on that - Have to contact them.
0
 

Author Comment

by:shahjagat
Comment Utility
I am not using a microsft Exchange email server. We are using a 3rd party server.
0
 

Author Comment

by:shahjagat
Comment Utility
Hi ladarling,

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

Thanks
0
 

Author Comment

by:shahjagat
Comment Utility
Hi ,  I do not see Mark As Answer buttons.

Please award  points to ladarling - Comment ID: 33639661

Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now