Link to home
Create AccountLog in
Avatar of eadam-uk
eadam-ukFlag for United States of America

asked on

SMTP - because the target machine actively refused it

Hi All,

I have created a script in asp.net & VB that uses the System.Net.Mail class to create an email and send it to a mail server that my ISP hosts. Every time I run the script I get the error message:

@No connection could be made because the target machine actively refused it xxx.xxx.xxx.xxx:25

I have done some reading around it and have tried all the suggestions such as: checking/adding credentials, checking firewall, etc. I have also worked with the ISP on different IP's and ports, but still no job. I have set my machine as the DMZ on my router in the hope that would circumnavigate any router problems, and have also published the site to the web server but each time the issue arises.

Any ideas?
Avatar of aibusinesssolutions
aibusinesssolutions
Flag of United States of America image

You'll need to check with your hosting company and see what mail server you should use.

Some hosting companies require you send to "smtp.domain.com", with SMTP authentication, some require you send to "localhost".
Avatar of eadam-uk

ASKER

I have worked with the hosting company and they have given me the mail server, and told me it's authenticated, I am using the following script for that part:

Dim smtp As New SmtpClient("67.15.97.8")
smtp.Credentials = New NetworkCredential("username", "password")
smtp.Port = 23 'I have tried it with and without this line, as well as port 587
smtp.Send(objMail)

Is there a way I can test this on a dev smtp server on my machine? I am using VS2008 and their temporary web server that creates itself when you hit view in browser. I assume this doesn't have an smtp element to it. Therefore do I have to install the full IIS with smtp element (Win XP) if I wanted to test this on my local machine - or would that not still work. At least then I guess we could rule out the code or anything my end.
If you are using that IP, and smtp authentication, you can test it from anywhere.  You are using the wrong port though, 23 is telnet, 25 is SMTP.  If you don't assign a port it defaults to port 25.

Also, you may want to set UseDefaultCredentials to false though, something like this.

Dim smtp As New SmtpClient("67.15.97.8")
smtp.UseDefaultCredentials = False
smtp.Credentials = New NetworkCredential("username", "password")

Also you can use a Try/Catch block on the Send to catch errors.

Try
    smtp.Send(objMail)
Catch ex As Exception
    'send error to a label or something ex.:
    'Label1.Text = "Error: " & ex.Message
End Try
Ok, I seem to be a little bit closer now.  I no longer get the error above, but the emails are not getting through. My ISP test a page in asp and their's works, see below. Mine is asp.net but I am hoping someone will spot the difference as they are pretty simple scripts.

My script now says that it has gone through (at least it doesn't produce an error like  before), but no emails arrive. I test the ISP script straight after, and then arrive. Most anoying! lol.
    Sub Send2Mail(ByVal sender As Object, ByVal e As EventArgs)
 
        Try
            Dim objMail As New MailMessage()
 
            objMail.To.Add("test@test.com")
            objMail.From = New MailAddress(strEmail.Text)
 
            objMail.Priority = MailPriority.Normal
            objMail.Subject = strSubject.Text
            objMail.IsBodyHtml = False
 
            objMail.Body = "Name : " + strName.Text + vbNewLine + "Email : " + strEmail.Text + vbNewLine + "Message : " + strYourMsg.Text
 
            Dim smtp As New SmtpClient("xx.xx.xx.xx")
            smtp.Port = "25"
            smtp.UseDefaultCredentials = False
            smtp.Send(objMail)
 
            strMessage.Text = "<p id='feedback'>- Thanks for your kind message.</p>"
            feedbackForm.Visible = False
 
        Catch ex As Exception
            strMessage.Text = "<p id='feedback'>- Error: " & ex.Message & "</p>"
 
        End Try
 
    End Sub
 
ISO scrip in asp:
 
<%
Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "Example CDO Message" 
objMessage.From = "tester@test.com" 
objMessage.To = "test@test.com" 
objMessage.TextBody = "cdo message"
 
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
 
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "xx.xx.xx.x"
 
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
 
objMessage.Configuration.Fields.Update
 
'==End remote SMTP server configuration section==
 
objMessage.Send
If Err <> 0 Then 
	Response.Write "An error occurred: " & Err.Description 
else
	Response.Write "Successfully Sent"
End If 
%>

Open in new window

Oh dear, the silence isn't looking good.
ASKER CERTIFIED SOLUTION
Avatar of aibusinesssolutions
aibusinesssolutions
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
You have this: objMail.From = New MailAddress(strEmail.Text)

Their server may only allow "From" addresses from local email accounts on their email server, since they are not using SMTP auth.

If you want to be able to reply to a user that sends an email, just add this.

objMail.From = New MailAddress"tester@test.com")
objMail.ReplyTo = New MailAddress(strEmail.Text)