使用PowerShell V4通过gmail发送邮件 [英] Send mail via gmail with PowerShell V4

查看:399
本文介绍了使用PowerShell V4通过gmail发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  $ 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)

我将$ EmailTo更改为我的电子邮件地址,将YOURUSERNAME / YOURPASSWORD更改为我的正确登录信息。

我知道这是上个月发生的事实。我最近更改了Gmail密码,显然此脚本停止工作。但是,我将YOURPASSWORD更改为正确的新版本,现在出于某种原因,在打开时出现错误:

  $ SMTPClient.Send($ EmailFrom,$ EmailTo,$ Subject,$ Body)
使用4参数调用Send的异常:SMTP服务器需要安全连接或客户端不是
$ authenticate。服务器响应是:5.5.1需要身份验证。详细信息请参阅
在线:9 char:1
+ $ SMTPClient.Send($ EmailFrom,$ EmailTo,$ Subject,$ Body )
+ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~
+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:SmtpException

我在Windows 7旗舰版64位上,我相信我是在PowerShell V2上,但今天更新到V4,因为我认为这可能是问题所在。

我的Gmail设置中也打开了不安全的应用程序,因为有人说这可能是问题所在。这不是,直到今天,我仍然得到错误。

您是否知道是否仍有可能通过PowerShell发送Gmail?或者我只是想念一些东西?谢谢你的帮助!

解决方案

我遇到了同样的问题,但Gmail向我发送了一封包含安全性较低的应用链接的邮件,然后它工作。这里的链接只需更改用户名和您的用户名为谷歌。

  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 


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!

解决方案

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

这篇关于使用PowerShell V4通过gmail发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆