3

I'm developing a simply monitoring too for my home and I need it to send me an email. However, no matter what I try, it is not connecting to my gmail account to send the notification - or even a simple test message. I have a script that is working for a utility I wrote for work, but that is using the corporate SMTP relay. I'm not going to mix work and personal resources.

I have spent the past 4-5 weeks digging through this and trying several other development languages. All have come up with similar problems.

I have attempted the following:

  • Turned on less secure apps
  • Tried a variety of ports, 25, 465, 587
  • Turned on and off SSL
  • Tried various forms of my username (email address)
  • Turned on 2-factor authentication and created an app password for this utility
  • Attempted to turn on TLS - as one error indicated I needed a starttls command.

With Powershell, I seem to get two common errors. The first one is dependent on the parameters I'm sending to the Send argument - regardless, it fails.

[Screenshot of error - ][1]

I have been on multiple forums and sites. All of the suggested code appears to be all variants of the same structure, as is the working one for my office - just using our own mail system.

Cœur
  • 37,241
  • 25
  • 195
  • 267
CCollins
  • 41
  • 1
  • 3
  • 1
    I think you need to re-link your screenshot. – codewario Jul 03 '18 at 15:44
  • If that linked question does not help you show us the code you are trying and any errors that you are getting. Currently we see no errors. – Matt Jul 03 '18 at 18:08
  • I realize that this question has been asked before. I have gone through all of the linked suggestions folks have posted on Stack Overflow that I can find, and it is still not working. Disabling 2 factor and enabling less secure apps does not work, changing port numbers is not working. posting my code would simply be posting links to the "Solutions" already posted here on Stack Overflow. I'll try to get my error messages re-posted. – CCollins Jul 04 '18 at 16:04
  • One of the common error messages I'm getting: Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed. + Send-MailMessage @sendMailArguments + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcepti on + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage – CCollins Jul 04 '18 at 16:14
  • Looks like this was marked as a dupe, but both the linked question and my answer here have the correct programmatic solution. The error you are receiving indicates that your script cannot communicate with the SMTP server. You may want to pose this question on the ServerFault or SuperUser StackExchange sites, posed as a question to why you can't communicate with the target SMTP server. – codewario Jul 06 '18 at 18:00

1 Answers1

2

You should be able to use Send-MailMessage for this. Here's an example for sending an email securely using Gmail's SMTP server (including how to securely store your credential for further use later or in automation):

# Securely enter your Gmail credential
$cred = Get-Credential

# Store it off in a file if you want to reuse it later from the same box
# Password is encrypted and only your user on the same box can decrypt it
$cred | Export-CliXml \Path\To\GmailCredential.xml

# Import the credential from the file later
$cred = Import-CliXml \Path\To\GmailCredential.xml

# Send the mail message (use -BodyAsHtml instead of -Body if you have an HTML body)
$sendMailArguments = @{
  SmtpServer = "smtp.gmail.com"
  Credential = $cred
  UseSsl = $true
  Port = 587
  To = "mailbox@domain.tld"
  From = "youremail@domain.tld"
  Subject = "Subject of the email"
  Body = "Body of the email"
}
Send-MailMessage @sendMailArguments

Note that if you have Multi-Factor Authentication (MFA) set up you will need an app-specific credential to auth to Gmail, which would be a required step as well for setting up an email client for a Gmail account using MFA.

If you can't reach smtp.gmail.com, then it's likely you have a firewall blocking traffic to that server and port.

And if you want to better understand why I'm calling Send-MailMessage with a hashmap of arguments instead of specifying each parameter separately, see the comment discussion on this answer, and read up on Splatting here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6

codewario
  • 19,553
  • 20
  • 90
  • 159
  • I have tried the suggested code, as well as any that I can find. I have spend the past month digging through Stack Overflow and every "Solution" I have come across is either a duplicate / derivative of the same response. I'm getting one error on some suggestions that there is something wrong with the parameters for the sending command, and other times I'm getting the error that basically it can't authenticate. I do not have a firewall set up on the network I'm testing from and I can get responses from smtp.gmail.com if I simply try to detect it. – CCollins Jul 04 '18 at 16:08
  • Getting the following error, which is one of the common ones I'm getting from the code offered on this site: Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed. + Send-MailMessage @sendMailArguments + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcepti on + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage – CCollins Jul 04 '18 at 16:12
  • That error means it can't talk to the SMTP server. You might try forcing the TLS protocol with `[System.Net.ServicePointManager]::SecurityProtocol = 'Tls,TLS11,TLS12'` before you run `Send-MailMessage`, or try port 465 instead. – codewario Jul 05 '18 at 13:48