0

I'm trying to send an email using C# console but failing each time with error says:

Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions 173.194.65.109:587

this is my C# code:

SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 587;

// setup Smtp authentication
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("myEmail@gmail.com", "myPassword");
client.UseDefaultCredentials = false;
client.Credentials = credentials;

MailMessage msg = new MailMessage();
msg.From = new MailAddress("myEmail@gmail.com");
msg.To.Add(new MailAddress("destinationEmail@gmail.com"));

msg.Subject = "This is a test Email subject";
msg.IsBodyHtml = true;
msg.Body = string.Format("<html><head></head><body><b>Test HTML Email</b></body>");
client.Send(msg);

I've also tried to use:

client.UseDefaultCredentials = true;

but still no success.

this is a generic C# code that is written all over the web, in each forum...but it does not help me.

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
Blaugrana
  • 1
  • 2
  • 1
    you must specify a different port – JC Borlagdan Apr 06 '16 at 13:15
  • Your code works fine for me. You do not need to change the port as suggested. Possible server problems at GMail this morning? Try it again. Outgoing Mail (SMTP) Server: smtp.gmail.com - Use Authentication: Yes - Port for SSL: 465 or 587 – ShaneLS Apr 06 '16 at 15:04
  • If you're using a home internet, some providers are really strict on what ports are open. – Henry Apr 06 '16 at 19:58

2 Answers2

0

i can think of 2 problems here. First port 587 is not open, this is higly unlikely since it's a standard port for mail but you never know.

second your code seems fine but you might have forgotten some parts in your app.config file. Something like this i suspect.

 <system.net>
  <mailSettings>
    <smtp>
      <network host="mail.OurOrganisationEmail@ourdomain.com" port="9999" userName="OurOrganisationEmail@ourdomain.com" password="OurOrganisationEmailPassword"/>
    </smtp>
  </mailSettings>
</system.net>
Axel Bouttelgier
  • 173
  • 1
  • 12
  • I apologize for the downvote. I got a little clicky, hit it by accident and didn't notice until just now. – ShaneLS Apr 07 '16 at 15:08
0

The only reason I can think of is that your network (software or hardware firewall, router, or modem) is blocking outgoing traffic on 587 or GMail servers were having issues yesterday. However, 587 is a standard SMTP SSL port and don't see a reason it would be blocked as it would also be used by email clients such as MS Outlook. Otherwise, your code is fine (it tested ok from my office). You're correct, this is pretty standard code for sending email via .net's SmtpClient. You may want to try GMail's port 465 (nonstandard, but sometimes used for legacy reasons) instead just to see if you can get out on that.

GMail Support (https://support.google.com/mail/troubleshooter/1668960?hl=en&rd=1) -

Outgoing Mail (SMTP) Server:
   smtp.gmail.com
   Use Authentication: Yes
   Port for SSL: 465 or 587

FYI, you can drop both:

client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;

They are not needed for an SMTP to GMail. The UseDefaultCredentials flag can actually cause login problems if you set it AFTER you set Credentials (which you have not done and would not be your current issue). And DeliveryMethod would only be needed if you have changed the app's (config) default delivery method already set to SmtpDeliveryMethod.Network.

ShaneLS
  • 466
  • 6
  • 14