Powershell电子邮件SMTP异常

Powershell电子邮件SMTP异常,powershell,email,smtp,powershell-2.0,powershell-4.0,Powershell,Email,Smtp,Powershell 2.0,Powershell 4.0,我正在尝试从windows server 2012 R2(PowerShell版本4)运行发送电子邮件的PowerShell脚本。我可以远程登录smtp.gmail.com端口587。 但是,电子邮件没有被发送出去 下面是代码片段和异常 $RPT_PWD='userpass' $Attachment = $args[2] $EmailTo = $args[1] $Subject = $args[0] $EmailFrom = "usertest@gmail.com" $SmtpServer =

我正在尝试从windows server 2012 R2(PowerShell版本4)运行发送电子邮件的PowerShell脚本。我可以远程登录smtp.gmail.com端口587。 但是,电子邮件没有被发送出去

下面是代码片段和异常

$RPT_PWD='userpass'
$Attachment = $args[2]
$EmailTo = $args[1]
$Subject = $args[0]
$EmailFrom = "usertest@gmail.com" 
$SmtpServer = "smtp.gmail.com"
$SmtpPort = "587"
$SMTPClient = New-Object System.Net.Mail.SmtpClient($SmtpServer, $SmtpPort) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object 
System.Net.NetworkCredential($EmailFrom,$RPT_PWD); # credentials to be used
$objMessage = New-Object System.Net.Mail.MailMessage
$Body = "Email Body" 
$objMessage = New-Object System.Net.Mail.MailMessage –ArgumentList 
$EmailFrom, $EmailTo, $Subject, $Body
$objMessage.Attachments.Add($Attachment)
$SMTPClient.Send($objMessage)


Exception calling "Send" with "1" argument(s): "Failure sending mail."
At E:\Scripts\DailySystemProcessCheck\EmailTest\mailx_ssl.ps1:133 char:2
+     $SMTPClient.Send($objMessage)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException
此外,我还尝试使用
发送邮件消息
,但也不起作用

$Credentials = New-Object System.Management.Automation.PSCredential -
ArgumentList $EmailFrom, $($RPT_PWD | ConvertTo-SecureString -AsPlainText -Force)

send-mailmessage -from $EmailFrom -to $EmailTo -subject $Subject -body $Body 
-Attachment $Attachment -smtpServer smtp.gmail.com -Credential $Credentials 
-UseSsl -Port 587

~~~~

send-mailmessage : The client and server cannot communicate, because they do 
not possess a common algorithm
At E:\Scripts\DailySystemProcessCheck\EmailTest\mailx_ssl.ps1:131 char:2
+     send-mailmessage -from $EmailFrom -to $EmailTo -subject $Subject -body 
$Body -A ...
+    
+ CategoryInfo          : InvalidOperation: 
(System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId : 
SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
我尝试使用“Powershell--Version2.0”运行脚本,现在我没有看到SMTP异常,而是看到另一个异常

Send : Exception calling "Send" with "1" argument(s): "A from address must 
be specified."
At E:\Scripts\DailySystemProcessCheck\EmailTest\mailx_ssl.ps1:124 char:18
+     $SMTPClient.Send <<<< ($objMessage)
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Send:使用“1”参数调用“Send”时出现异常:“发件人地址必须为”
必须具体说明。”
在E:\Scripts\DailySystemProcessCheck\EmailTest\mailx\u ssl.ps1:124 char:18

+$SMTPClient.Send我想看看这里:您应该使用Send emailmessage。检查客户端和服务器上的TLS设置。例如,请参见问题。