locked
No connection could be made because the target machine actively refused it

    Question

  • I made a simple email form and keep getting the error 'No connection could be made because the target machine actively refused it. any help would be great thanks. using System.Net.Mail.

     

    protected void Button1_Click(object sender, System.EventArgs e)

    {

    SmtpClient smtpClient = new SmtpClient();

    MailMessage message = new MailMessage();

    MailAddress fromAddress = new MailAddress(TextBox2.Text, TextBox1.Text);

    smtpClient.Host = "localhost";

    smtpClient.Port = 25;

    message.From = fromAddress;

    message.To.Add("someone@somebody.net");

    message.Subject = "Feedback";

    message.Body = TextBox3.Text;

    smtpClient.Send(message);

    }

    Sunday, May 20, 2007 11:30 PM

Answers

  • Found out a way to dynamicallly change the Host property at design time so the problem is solved. Just changed the Text property to     Host.

     

    Thanks for eveyones help on this issue.

    Wednesday, June 06, 2007 8:07 PM

All replies

  • The server might not have mail server running on port 25. Check it.
    Monday, May 21, 2007 10:00 AM
  • I checked it does have mail running on port 25.
    Tuesday, May 22, 2007 11:52 AM
  • Does:

     

    telnet localhost 25

     

    work from a command prompt?

    • Proposed as answer by Mayank-Sri Wednesday, November 19, 2008 5:10 PM
    Tuesday, May 22, 2007 1:01 PM
  • Make sure you indeed have a relay server running on port 25. Try using the local machine name or the fully qualified domain name of your machine instead of "localhost". Or use the IPv4 address.

    Then make sure you have the right credentials - your SMTP relay server may not support ananymous requests.

    NetworkCredential myCreds = new NetworkCredential(YourUserName@servername.com, "YourPassword", "");           

                smtpClient.Credentials = myCreds;

    or

    smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;

     

    If this doesn't work try this code - it'll use gmail as a relay server instead of your local SMTP server/service.

    http://blogs.msdn.com/mariya/archive/2006/06/15/633007.aspx

     

    Let me know if any of those worked

     

    Mariya

    Wednesday, May 23, 2007 12:55 AM
    Moderator
  • The problem is alot of other people are going to be using this control it has to be localhost. I remember on XP you could set a relay.Why not on Vista. The smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials did not work. I'm wondering If there is a way to add a dialog box to ask to user to add their smtp host like the login in control without just adding another text box asking for it. which is something I really don't want to do. By the way it works when you use another host like mail.somewhere.net. Just not localhost. And this is part of a web control that i'm making.
    Thursday, May 24, 2007 12:35 PM
  • "The problem is alot of other people are going to be using this control it has to be localhost."

    I'd say that could be a problem as well - what if my mail server is not localhost? You need some way to configure that setting regardless, I think...

     

    Now, you never actually mentioned this was Vista before, and I can now tell you that the SMTP server has been removed from Vista (I don't know why). What, exactly, did you check when you said "I checked it does have mail running on port 25." because that's not going to be the case on Vista (unless you have a third-party SMTP server). Did you try the "telnet localhost 25" suggestion I made earlier?

     

    If you're trying to do this on Vista, then as there is no SMTP server included with Vista, you're going to have to come up with some other solution, I'm afraid.

    Monday, May 28, 2007 3:16 AM
  • Found out a way to dynamicallly change the Host property at design time so the problem is solved. Just changed the Text property to     Host.

     

    Thanks for eveyones help on this issue.

    Wednesday, June 06, 2007 8:07 PM
  • Try using the Telnet comment mentioned above.
    If you have an anti-virus running, check the Access Protection Logs, you might find the problem (like in my case).

    Good Luck!!!
    Wednesday, November 19, 2008 5:13 PM
  • Antivirus may be blocking the send email request please check the antivirus logs and add exclusion for WebDev.WebServer.exe in your anitvirus software for II7.
    • Proposed as answer by Shoaib Faruq Tuesday, December 14, 2010 3:33 PM
    Tuesday, December 14, 2010 3:32 PM