views:

112

answers:

4

I'm using the following code which appears to work perfectly every time on Vista/Win7.

private void SendEmail(string subject, string body, string attach)
{
    using (MailMessage message = new MailMessage("[email protected]", "[email protected]", subject, body))
    {
        message.IsBodyHtml = true;

        if (!string.IsNullOrEmpty(attach))
        {
            Attachment attached = new Attachment(attach);
            message.Attachments.Add(attached);
        }

        SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential("[email protected]", "password"),
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network
        };

        client.Send(message);
    }
}

However on Windows XP I'm getting:

No connection could be made because the target machine actively refuses it

I've checked and Windows firewall is completely disabled...

+2  A: 

Try from the Windows machine the following:

  1. windows key + r
  2. Type cmd
  3. Type telnet smtp.gmail.com 587

If it says connection refused or similar then it's a firewall or network problem, unrelated with the code.

Andreas Bonini
telnet fails. For some reason this happens with all our XP machines and none of the Vista Win7 ones. I'll have to see what they all have installed...
Tim
+1  A: 

Hard to say if this is it, but we had that problem at one point, and it was an antivirus utility that was the culprit.

Mark Wilkins
A: 

Are you using the same version of System.Net.Mail on all three systems ?

Also, could be related to the Windows Firewall blocking connections (or some other firewall).

Nick Haslam
A: 

I doubt this has anything to do with the OS, that type of exception is usually bubbled up from inner ones. Trap the exception and have a look into the inner exceptions and see what the real problem is.

However, this sort of problem is usually a firewall blockage, the remote smtp server is blocking incoming requests or your machine is blocking outgoing requests on port 25.

James