Send mail via gmail with PowerShell V4

我们两清 提交于 2020-01-02 19:26:33

问题


I have been using this PowerShell script to send my an email to me when opened.

$EmailFrom = "notifications@somedomain.com"
$EmailTo = "anything@gmail.com" 
$Subject = "sample subject" 
$Body = "sample body" 
$SMTPServer = "smtp.gmail.com" 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("YOURUSERNAME", "YOURPASSWORD"); 
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

I changed the $EmailTo to my email address, and the YOURUSERNAME / YOURPASSWORD to my correct login information.

I know as a fact that this was working last month. I recently changed my Gmail password, and obviously this script stopped working. However, I changed the YOURPASSWORD to the correct new one, and now for some reason I am getting an error when opening:

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Exception calling "Send" with "4" argument(s): "The SMTP server requires a secure connection or the client was not 
authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
At line:9 char:1
+ $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SmtpException

I am on Windows 7 Ultimate 64bit, and I believe I was on PowerShell V2, but today updated to V4 due to me thinking that might have been the issue.

I have also turned on unsecured apps in my Gmail setting since someone said that might be the issue. It was not, and to this day, I am still getting the error.

Do you know if it is still possible to send Gmails through PowerShell? Or am I just missing something? Thanks for your help!


回答1:


I had the same issue but Gmail send me a mail with a link for "Less secure app", I put it on ON then it worked. Here the link just change USERNAME with your username for google.

https://accounts.google.com/AccountChooser?Email=USERNAME@gmail.com&continue=https://www.google.com/settings/security/lesssecureapps?rfn%3D27%26rfnc%3D1%26eid%3D3301110864727181130%26et%3D0%26asae%3D2


来源:https://stackoverflow.com/questions/34972917/send-mail-via-gmail-with-powershell-v4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!