views:

597

answers:

6

i have the following code below (i have stuck in xxx to not publish the real name). this code works fine when i upload to the server and will send out emails perfectly.

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(fromEmailAddress_);
        msg.To.Add(new MailAddress(toEmailAddress_));

        msg.Subject = subject_;
        msg.Body = body_;
        msg.IsBodyHtml = true;
        msg.Priority = MailPriority.High;

        NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("[email protected]", "password");
        try
        {
            SmtpClient c = new SmtpClient("mail.xxx.org");
            c.UseDefaultCredentials = false;
            c.Credentials = basicAuthenticationInfo;
            c.Send(msg);
        }
        catch (Exception ex)
        {
            throw ex;
            Console.Write(ex.Message.ToString());
        }

but when i test locally on my machine in visual studio, i get an error:

[SocketException (0x274d): No connection could be made because the target machine actively refused it 66.226.21.251:25] System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +239 System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) +35 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +224

[WebException: Unable to connect to the remote server] System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) +5420699 System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) +202 System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +21 System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +332 System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) +160 System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) +159

any suggestions of why this wouldn't work

A: 

The provider has probably firewalled off their mail server so that it is only accepting mail from their hosting environment.

Change it to use your local mail server when you're testing it locally.

caf
does windows xp come with some type of mail server that i can use locally for testing?
ooo
Just use your ISP's server, the one that's configured in your mail client.
caf
+2  A: 

The first thing I'd do to run this down is to see whether the target machine is, as the error message says, actually refusing the connections. I can connect to port 25 on that IP address from here, so it's possible that your error message is misleading. These are all possible causes of a failure to connect:

  • there's some problem in your code
  • there's some problem in the library you're using
  • you have a local firewall preventing outbound mail from strange programs
  • your ISP does not permit outbound SMTP connections except to their own servers
  • some intermediate network is blocking things
  • the mail server's ISP is blocking SMTP connections from your network
  • the mail server really is rejecting you, perhaps because you're on a blocklist

To get more detail, download and install Wireshark. It's like a debugger for networks. Start a capture between your system and the target network and then run your code. Interpreting the captured packets takes some experience, so if you get stuck feel free to post more data here.

William Pietri
+1  A: 

Relay restrictions are most likely preventing you from sending mail from outside an allowed set of IPs. If the SMTP Server is configured on a web/application server, it's probably set up to allow mail to be sent via the 127.0.0.1 only.

You can configure a local SMTP Server via the IIS Manager. If you go down this path, make sure port 25 is opened up on your local machine. Often times anti-virus type software block/monitor this port and it mucks with email delivery.

Ben Griswold
A: 

You asked for an XP Mail Server. Try hMailServer

devio
A: 

Have a look to see if your antivirus is going crazy, i've have seen this problem a few times with people running mcafee.

Hope this helps.

+1  A: 

I think you are not using the port no in your code. Use these lines, Then mail should be gone on local also.

    smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
    smtp.Timeout = 60000;
    smtp.Port = 25;

This is working on my system. I hope it will work also on your local system.

Deepak