-2

The code works on a local machine, but one the target machine I get:

"Unable to connect to the remote server"

and

"An attempt was made to access a socket in a way forbidden by its access 
permissions    xxx.xxx.xxx.xxx:587"

Could permission be denied for doing this on this machine? I can get out to gmail via a web browser.

Code is like this (from SO):

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your mail@gmail.com");
mail.To.Add("to_mail@gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
mail.Attachments.Add(attachment);

SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);

It works on one machine, but not another, so I'm assuming some network permission restriction.

Jiminion
  • 5,080
  • 1
  • 31
  • 54
  • Millions of possible issues here. [What have you tried?](http://stackoverflow.com/questions/15619921/an-attempt-was-made-to-access-a-socket-in-a-way-forbidden-by-its-access-permissi) – TheNorthWes Jun 18 '14 at 22:13
  • Same code worked on (my) local machine. I'm basically wondering if a permission could be set to allow only certain applications access to a socket. Or maybe some login permission is read. I don't know. – Jiminion Jun 18 '14 at 22:20
  • 1
    The network admins might have locked down that port without certain permissions. – Tacoman667 Jun 18 '14 at 22:21
  • Absolutely. It totally depends on the security context and permissions of the user / app – TheNorthWes Jun 18 '14 at 22:23
  • @Tacoman667, so the net admins could disable a call to gmail via a C# program but still allow gmail access via a Browser? – Jiminion Jun 18 '14 at 23:07
  • 1
    A browser goes over a different open port. 587 is a specific port used mostly for smtp. Any port can be repurposed for any reason, even for malicious intent. For that reason, network admins usually block all ports and whitelist then as needed. Gmail in the browser is like looking at any other website. – Tacoman667 Jun 19 '14 at 12:19
  • @Jim you have to talk to your network admins. We can't really do much other than that. Unless you start hacking around this you likely won't solve it, and if you do, you may trip red alarms which the netadmins will notice and then block your hack. – TheNorthWes Jun 19 '14 at 16:13

3 Answers3

1

Can you adjust whatever your SendMail method is using to explicitly use port 25 instead 587? Given your comment about gmail, are you providing a username/password with the message you are sending? If you post the code snippet where you construct/send your message it will make troubleshooting much easier.

Edit: To really test to see if that port is being blocked by your network admins, use telnet. It's free: http://technet.microsoft.com/en-us/library/cc771275(v=ws.10).aspx

Fire up a cmd prompt, and try to connect to your mail server on the common SMTP ports. This will tell you definitively if you are being blocked. For a command reference on how to connect to something on a particular port, see this TechNet article: http://technet.microsoft.com/en-us/library/bb491013.aspx

Bill Sambrone
  • 4,334
  • 4
  • 48
  • 70
  • Yes, I tried port 25. And I did provide user/psswd, which worked on a different system. So I think it is a network admin thing. – Jiminion Jun 18 '14 at 23:06
1

Have you checked the firewall & AV software of the machine that it fails on? I've been bitten by this before.

Patrick Allwood
  • 1,822
  • 17
  • 21
1

It looks as though you will need to speak with your network admins to make sure all required ports are opened on that server. They block ports by default and whitelist them as needed.

Tacoman667
  • 1,391
  • 9
  • 16