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

    Question

  • I am using the example in WROX 'Professional ASP.NET4 in C# and VB'. (Excellent book). (Using Visual Studio 2010 Professional ASP.NET 4)

    On  page 977 Sending Mail, there is an example of 'Sending Mail'.

                System.Net.Mail.MailMessage message =
                    new System.Net.Mail.MailMessage("xxxx@yahoo.com", "yyyy@yahoo.com");
                message.Subject = "Sending Mail with ASP.NET";
                message.Body =
                    "This is a sample email which demonstrates sending email using ASP.NET";
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");
                smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
                try
                {
                    smtp.Send(message);        // fails here without invoking the next instructions
                }
                catch (SmtpFailedRecipientsException ex)
                {
                    for (int i = 0; i < ex.InnerExceptions.Length; i++)
                    {
                        SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
                    }
                }

    In Control Panel - Administrative Tools - Internet Information Services 7 - I have ASP.NET SMTP-Email with Deliver E-Mail to SMTP locasl server localhost port 25.

    In WEB-Config

     <system.net>
        <mailSettings>
          <smtp from="">
            <network defaultCredentials="true" host="localhost" password=""
              userName="" />
          </smtp>
        </mailSettings>
      </system.net>

    I have tried with both firewalls on and off. Same thing.

    I have seen many desciptions of this problem, but no resolution.

    How do I send any email from ASP.NET4?

    SealSplash

    Thursday, April 19, 2012 3:13 AM

Answers

  • It is working. Thank you Joel. I got the Message Sent, and it showed up in my yahoo.account.

    Note, that I had to replace my smtf with "localhost". Using smtp.larsmpedersen.com would result in

    Mailbox unavailable. The server response was larsmpedersen@yahoo.com. No such user here.

    <%@ Import Namespace="System.Net" %>
    <%@ Import Namespace="System.Net.Mail" %>
    <%--www.larsmpedersen.com/emails
    --%>
     <script language="C#" runat="server">
         protected void Page_Load(object sender, EventArgs e)
         {
             MailMessage mail = new MailMessage();
             mail.From = new MailAddress("larsmpeders@larsmpedersen.com");
             mail.To.Add("larsmpedersen@yahoo.com");
             mail.Subject = "This is an email from script";
             mail.Body = "this is the body content of the email script.";
             //SmtpClient smtp = new SmtpClient("smtp.larsmpedersen.com", 25);
             SmtpClient smtp = new SmtpClient("localhost", 25);       <--- use localhost
             smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
             smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
             NetworkCredential netCred = new NetworkCredential("smtp.larsmpedersen.com", "xxxxxxxx");
             smtp.Credentials = netCred;
             smtp.Send(mail);
             lblMessage.Text = "Mail Sent";
         }
     </script>
     <html>
     <head><title>email test</title>
     <body>
         <form id="Form1" runat="server">
             <asp:Label id="lblMessage" runat="server"></asp:Label>
         </form>
     </body>
     </html>

    • Marked as answer by sealsplash Thursday, April 26, 2012 11:37 PM
    Thursday, April 26, 2012 11:28 PM

All replies

  • The webpage below has a list of the Well Known Port Numbers

    http://www.tcp-ip-info.de/tcp_ip_und_internet/well_known_ports.pdf

    Networks/firewalls attempt to block IP messages with by using the port number.  since they also need to allow messaes to pass through they usually allow port number that aren't weel known (not in the list).  One thing you can try is to use a port number above 10,000 that isn't on the list and see if this works.


    jdweng

    Thursday, April 19, 2012 8:35 AM
  • I tried to change the localhost port adress to 10100 (both in IIS 7 and in the program).

    Same result.

    I find it hard to understand there are so many people with the same problems. I see them everywhere in MSDN. Yet, nobody has an explanation for it.

    Microsoft should have an explanation, and what to do. I have to give up mthis approach and move on.

    Friday, April 20, 2012 12:14 AM
  • I guess that is why the are forums!  Microsoft doesn't do a great job of documenting there libraries.    The wording is poor and  and is missing important details.  the best way of learning is from the posted examples, but the examples are often incomplete and don't work.

    With networking issues there are many levels to the network software which can create the same error, and the login credentials can also produce similar type errors as the programming errors.  Then the error messages are intentionally vague to make it difficult for hackers to breach a website.

    I usually add some of the variables to the watch window and look for indications of what caused the error.  Any internet method should have a top level server/client with a lower level port.  I check to make sure there is a valid port with a source IP, Destination IP, and port number. there may be a better description exception error in the port than in the client/server.  I also check to see which properties are set to null. 

    I also use these DOS commands

    1) netstat -a : gives a list of connections.  Sometimes the port number you are trying to use is already being used by another process.

    2) IPConfig /all :  give a list of all interfaces.  In yoiu case I would check the local host.

    3) arp -a : give a list of the arp table.  the arp table is filled with responses from a ping message and when computers are turned on.

    4) ping -a IP  : In you case I would ping local:host to see what your computer is using as the local:host.  Then check the IP Address and mask in the IPCONFIG to see if there is a route to the mail server yahoo.  I use the -a option in ping to get the host name along with the ip address.


    jdweng

    Friday, April 20, 2012 7:06 AM
  • I am in Visual Studio 2010. How do I get to netstat ?

    Any other ways I can find out where the port is, and how I can manipulate them?

    Thanks, SealSplash

    Sunday, April 22, 2012 6:30 AM
  • You can connect to the port using the IPaddress Class (see website below).  You can get the list of active connections using this class.  I usally avoid this and use simple debug techniques.  When a failure occurs I put a break point into my software.  Then add a watch  to the variable returned by the constructor call to the Server/Client.  then I start look at the watch variables that are returned.  I especially check the private variables and the port object.  Usually there will be an error message that will give more details than the error message given by the exception.

     

    I often don't use the loopback (127.0.0.1) as the host for my client/server.  Instead I use one of the interfaces IP that you can see using the "IPConfig - ALL".  Notice Address[0] is an array and you will have all the interfaces in the array.

    I get the IP address using this code

    Get the local computer host name.
                    LocalHostName = Dns.GetHostName();
                    Console.WriteLine("Computer name :" + LocalHostName);
                    LocalHostIPEntry = Dns.GetHostEntry(LocalHostName);
                    LocalHostIP = LocalHostIPEntry.AddressList[0];

    http://msdn.microsoft.com/en-us/library/system.net.ipaddress(v=VS.100).aspx


    jdweng

    • Proposed as answer by duyhai707 Sunday, April 22, 2012 12:55 PM
    Sunday, April 22, 2012 12:04 PM
  •  I tried the following         

            string LocalHostName = Dns.GetHostName();
             Console.WriteLine("Computer name :" + LocalHostName);
             IPHostEntry LocalHostIPEntry = Dns.GetHostEntry(LocalHostName);
             IPAddress LocalHostIP = LocalHostIPEntry.AddressList[0];
             string netaddr = LocalHostIP.ToString();
              //create the mail message
             MailMessage mail = new MailMessage();
              mail.From = new MailAddress("xxxx@yahoo.com");
             mail.To.Add("yyyy@yahoo.com");
             mail.Subject = "This is an email from script";
             mail.Body = "this is the body content of the email script.";
             SmtpClient smtp = new SmtpClient(netaddr);         
             smtp.Send(mail);        <--- stops here
             lblMessage.Text = "Mail Sent";

    No connection could be made because the target machine actively refused it [fe80::488c:4f34:a37c:76c0%9]:25

    How do I find a port I can use?

    Monday, April 23, 2012 12:07 AM
  • My yahoo email account was disabled because I haven't used it in a while.  It will take up to 24 hours to get re-enabled so I couldn't test my changes.  I did some research on the internet and this is what I found

    1) I set the port to 25 just to make sure 25 was being used

    2) The delivery method needs to be set to Network.  Some one said that it wasn't needed in Net 3.5 but was required in Net 4.0

    3) I added the credentials so you can set your password.

    4) I put my yahoo email address into the cod.   change as required.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Mail;
    namespace ConsoleApplication1
    {
        class Program
        {
            
            static void Main(string[] args)
            {
             
             string LocalHostName = Dns.GetHostName();
             Console.WriteLine("Computer name :" + LocalHostName);
             IPHostEntry LocalHostIPEntry = Dns.GetHostEntry(LocalHostName);
             IPAddress LocalHostIP = LocalHostIPEntry.AddressList[0];
             string netaddr = LocalHostIP.ToString();
              //create the mail message
             MailMessage mail = new MailMessage(); 
              mail.From = new MailAddress("jdweng@yahoo.com");
             mail.To.Add("jdweng@yahoo.com"); 
             mail.Subject = "This is an email from script";
             mail.Body = "this is the body content of the email script.";
             SmtpClient smtp = new SmtpClient(netaddr,25);
             smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
             NetworkCredential netCred = new NetworkCredential("jdweng@yahoo.com", "Password");
             smtp.Credentials = netCred;
             smtp.Send(mail);       // <--- stops here
             //lblMessage.Text = "Mail Sent";
            }
            
        }
    }


    jdweng

    Monday, April 23, 2012 9:38 AM
  •  

     NetworkCredential netCred = new NetworkCredential("larsmpedersen@yahoo.com", "xxxxxxxx");
    Line 25:          smtp.Credentials = netCred;
    Line 26:          smtp.Send(mail);

    I still get the same result. Am I missing some other smtp parms?  In IIS7 I have SMTP E-mail set to 25 port address.

    No connection could be made because the target machine actively refused
    it [fe80::488c:4f34:a37c:76c0%9]:25

    Tuesday, April 24, 2012 7:23 AM
  • I realized last night that you have to specify in the smtpclient constructor the server name as "email.yahoo.com".  See this example

    http://msdn.microsoft.com/en-us/library/67w4as51(v=vs.90).aspx

    The code you are using is making your PC the smtp host.  Your PC doesn't have this service, and you want to use your account in yahoo.  So what you want to do is to connect to the yahoo host over the internet, then let yahoo actually sent your message using smtp.  I'm not sure what port number to use. It is not clear from the documentation if the port number specfied is the port number that is used when the conenction is made between your PC and yahoo.com or the port number is port number which yahoo uses to sned the email.  Yo may want to try a port number that is greater than 10,000 which will get through firewalls that attempt to block wll known port number.  I would try both 25 and 10,025.


    jdweng

    Tuesday, April 24, 2012 9:32 AM
  • I used my account at discountasp.net.

    <%@ Import Namespace="System.Net" %>
    <%@ Import Namespace="System.Net.Mail" %>

    <%--www.larsmpedersen.com/emails
    --%>
     <script language="C#" runat="server">
         protected void Page_Load(object sender, EventArgs e)
         {
             MailMessage mail = new MailMessage();
             mail.From = new MailAddress("larsmpedersen@yahoo.com");
             mail.To.Add("larsmpedersen@yahoo.com");
             mail.Subject = "This is an email from script";
             mail.Body = "this is the body content of the email script.";
             SmtpClient smtp = new SmtpClient("smtp.larsmpedersen.com", 25);
             smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
             NetworkCredential netCred = new NetworkCredential("smtp.larsmpedersen.com", "xxxxxxxx");
             smtp.Credentials = netCred;
             smtp.Send(mail);
             lblMessage.Text = "Mail Sent";
         }
     </script>
     <html>
     <head><title>email test</title>
     <body>
         <form id="Form1" runat="server">
             <asp:Label id="lblMessage" runat="server"></asp:Label>
         </form>
     </body>
     </html>

    The result: Mailbox unavailable. The server response was: <larsmpedersen@yahoo.com>
    No such user here.

    Now I have to check the email instructions at discountasp.net.

    Wednesday, April 25, 2012 11:51 PM
  • I see two problems with your code

    1) I think you need to set the following

    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    If you don't the code will use the default account on your PC which is setup in control Panel - Mail.  So if you have outlook setup on your PC to another POP account,  or mail server you will be using this email account.  Even is you don't have email setup on your PC the SMTP will fail.

    2) The FROM email address must match the login credentials.  Yo can't use a from address as yahoo.com and the logon credentials using discountasp.net.  If this was allowed hackers to easily send email a from phoney email address.


    jdweng

    Thursday, April 26, 2012 7:16 AM
  • It is working. Thank you Joel. I got the Message Sent, and it showed up in my yahoo.account.

    Note, that I had to replace my smtf with "localhost". Using smtp.larsmpedersen.com would result in

    Mailbox unavailable. The server response was larsmpedersen@yahoo.com. No such user here.

    <%@ Import Namespace="System.Net" %>
    <%@ Import Namespace="System.Net.Mail" %>
    <%--www.larsmpedersen.com/emails
    --%>
     <script language="C#" runat="server">
         protected void Page_Load(object sender, EventArgs e)
         {
             MailMessage mail = new MailMessage();
             mail.From = new MailAddress("larsmpeders@larsmpedersen.com");
             mail.To.Add("larsmpedersen@yahoo.com");
             mail.Subject = "This is an email from script";
             mail.Body = "this is the body content of the email script.";
             //SmtpClient smtp = new SmtpClient("smtp.larsmpedersen.com", 25);
             SmtpClient smtp = new SmtpClient("localhost", 25);       <--- use localhost
             smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
             smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
             NetworkCredential netCred = new NetworkCredential("smtp.larsmpedersen.com", "xxxxxxxx");
             smtp.Credentials = netCred;
             smtp.Send(mail);
             lblMessage.Text = "Mail Sent";
         }
     </script>
     <html>
     <head><title>email test</title>
     <body>
         <form id="Form1" runat="server">
             <asp:Label id="lblMessage" runat="server"></asp:Label>
         </form>
     </body>
     </html>

    • Marked as answer by sealsplash Thursday, April 26, 2012 11:37 PM
    Thursday, April 26, 2012 11:28 PM
  • Dear SealSplash

    Please Try this solution to solve the problem Hope so it will useful to you.

    Open The IIS ->Default SMTP Virtual Server->Properties->Access Tab->Relay Restriction->ADD the IP Address of Web Server or Domain Name Then ok and Apply the settings.

    Thanks

    Tushar Patil

    UAE.

    • Proposed as answer by dotnetRook Thursday, September 06, 2012 9:56 PM
    Wednesday, May 16, 2012 9:08 AM
  • Check if you can send more than email one message in a loop.  I think you will need to use dispose between each mail message to close the connection.

    jdweng

    Thursday, September 06, 2012 10:53 PM