1

Trying to send email using SmtpClient & MailMessage via SendGrid. It works on 1 PC, but not another.

  • Same latest build of Windows
  • Both using Powershell 5.1.19041.1151
  • Firewalls disabled on both
  • Both running Windows Defender no other AV
  • Both on the same LAN behind the same router
  • Tried SSL on or off and ports 587, 465, 25, 2525
  • I have run the equivalent c# code on the machine where Powershell fails and it works fine.
  • Tested on 10 diff client machines and only 2 of those didn't work, still can't find the common demoninator

The SmtpException object gives me:

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

  • Seems like Windows is blocking access to a socket request from Powershell.
  • Using Tcpview on the failing machine I can verify that no connection is made to remote smtp server.
$emailSmtpServer = "smtp.sendgrid.net"
$emailSmtpServerPort = "2525"

$emailSmtpUser = "user"
$emailSmtpPass = "pass"
 
$emailFrom = "test@test.com"
$emailTo = "test@test.com"

$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = "Test" 
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = "Test"

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $False
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$SMTPClient.Send( $emailMessage ) 
  • There seems to be a [few](https://stackoverflow.com/a/10461978/503046) strange [causes](https://stackoverflow.com/a/63009050/503046). Anyway, can you add the Powershell script to the question just for double-checking? – vonPryz Sep 01 '21 at 04:23
  • @vonPryz - code added per your request. Strange indeed. I set Defender disabled in GPO and it was still blocked. – wrenchMonkey Sep 01 '21 at 13:54
  • Any suggestions to get more attention? – wrenchMonkey Sep 02 '21 at 12:44

0 Answers0