0

Using this code (System.Net.Mail):

SmtpClient client = new SmtpClient();
client.Send(MyMessage);

I get this error:

System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:25

Web.config entries.

Working code.

        {
    string body = "";
    body = "<table border='0' align='center' cellpadding='2' style='border-collapse: collapse' bordercolor=''#111111' width='100%' id='AutoNumber1'>";
    body = body + "<tr><td width='100%' align='center' colspan='6'><b>Feed Back Form</b></td></tr>";
    body = body + "<tr><td width='100%' colspan='6'>&nbsp;</td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Name</td><td width='50%' colspan='4'><b>" + name.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Address</td><td width='50%' colspan='4'><b>" + Address.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>City</td><td width='50%' colspan='4'><b>" + City.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>State</td><td width='50%' colspan='4'><b>" + State.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Country</td><td width='50%' colspan='4'><b>" + Country.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Zip/Pin Code</td><td width='50%' colspan='4'><b>" + ZipCode.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Phone</td><td width='50%' colspan='4'><b>" + Phone.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>E-Mail</td><td width='50%' colspan='4'><b>" + email.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Website URL (If Any)</td><td width='50%' colspan='4'><b>" + weburl.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>How did you know about Country Oven?</td>";
    body = body + "<td width='50%'><b>" + radiobutn.SelectedItem.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Your feedback/suggestions for the site</td>";
    body = body + "<td width='50%' colspan='4'><b>" + txtsugg.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Query (If you have any)</td>";
    body = body + "<td width='50%' colspan='4'><b>" + query.Text + "</b></td></tr></table>";
    MailMessage message = new MailMessage();
    message.To = "contact@xxxx.com";
    message.From = email.Text;
    message.Subject = "ContactUs Form";
    message.BodyFormat = MailFormat.Html;
    message.Body = body;
    SmtpMail.SmtpServer.Insert(0, "");
    SmtpMail.Send(message);
    // lblmsg.Text = "Message sent successfully";
    RegisterStartupScript("startupScript", "<script language=JavaScript>alert('Message sent successfully.');</script>");
    clear();
}


 <system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
        <network defaultCredentials="True" host="LocalHost" port="25" />
        </smtp>
    </mailSettings>
</system.net>

What is wrong ?

Jack
  • 101
  • 1
  • 6

4 Answers4

2

Make sure your firewall is allowing the port 25. also SMTP service is configured and running in your local machine.

Bitlancer
  • 93
  • 1
  • 6
  • I have two aspx forms, one use web mail and one uses net.mail. The form using web mail still works.So, i dont think it is a services issue. – Jack May 26 '11 at 20:02
1

Maybe you are just not running an SMTP server on your localhost.

Mr47
  • 2,655
  • 1
  • 19
  • 25
1

You probably haven't got an smtp server running. Try if dumping the files on disk works:

<mailSettings>
  <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
   </smtp>
</mailSettings>

If this works, then try setting up a local mailserver and change the settings back to your current values.

These answers might help you further:

Community
  • 1
  • 1
Marijn
  • 10,367
  • 5
  • 59
  • 80
0

If it worked correctly with the same machine before you moved away from the deprecated method then I would assume that the default configuration for the .NET send mail is different than the default configuration for the older one.

Perhaps try removing the delivery method entry in the webconfig and use the default method.

I've connected to exchange this way before and never specified anything besides a from address, a host, username, and password.

Jeff Machamer
  • 942
  • 4
  • 7
  • I have two aspx forms, one use web mail and one uses net.mail. The form using web mail still works.So, i dont think it is a services issue. – Jack May 26 '11 at 20:00
  • I would lookup the default values between the old and new way and see how they differ. Since they are using the same configuration section of the web config it has to be a difference between the way the two of them behave, and I would assume that the biggest difference would be default configuration issues. – Jeff Machamer May 26 '11 at 20:06