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 )