2017-09-14 131 views
-1

错误PowerShell来发送邮件

新对象:无法找到 “PSCredential的” 过载和 争论数: “2”。在d:\脚本\ gsend.ps1:12字符:15

代码

#Create Password File (Only need once) 
#$Credential = Get-Credential 
#$Credential.Password | ConvertFrom-SecureString | Set-Content "D:\scripts\gsendcred.txt" 

#Send Email 

$EncryptedCredential = "D:\scripts\gsendcred.txt" 

$EmailUsername = "[email protected]" 

$EncryptedPW = Get-Content "D:\scripts\gsendcred.txt" 

$EncryptedCredential = ConvertTo-SecureString -String $EncryptedCredential - 
AsPlainText -Force 

$Credential = New-Object Management.Automation.PSCredential ($EmailUsername, 
$EncryptedPW) 

$EmailFrom = "[email protected]" 

$EmailTo = "[email protected]" 

$EmailSubject = "GSEND Test Subject" 

$EmailBody = "Test Body" 

$SMTPServer = "smtp.gmail.com" 

$SMTPPort = 587 

$SMTPSsl = $true 
+0

这是我收到的错误:新对象:无法找到“PSCredential的”和参数计数过载:“2”。 在D:\ Scripts \ gsend.ps1:12 char:15 –

+2

欢迎来到SO。请阅读以下内容,然后再向SO发布更多内容:https://stackoverflow.com/help/asking –

+1

使用您的$ EncryptedCredential而不是$ EncryptedPW,PSCredential对象需要SecureString –

回答

0

您可以使用以下PowerShell脚本用于发送邮件

发送- - 要MAILMESSAGE“ABC @ abc.com“-From”[email protected]“-Cc”[email protected]“,”[email protected]“ - 主题测试邮件-BodyAsHtml $ htmlbody -SmtpServer”mailhost.abc.com“

其中$ htmlbody是包含html的字符串变量。

0

我相信你的问题是处理凭据。下面是我如何处理SMTP凭证:

$smtpPwd = "password" 
$smtpCredentialsUsername = "[email protected]" 
$smtpCredentialsPassword = ConvertTo-SecureString -String $smtpPwd -AsPlainText -Force 

$Credentials = New-Object –TypeName System.Management.Automation.PSCredential 
    –ArgumentList $smtpCredentialsUsername, $smtpCredentialsPassword 

然后:

$smtp.Credentials = $Credentials 

或者,如果您使用的发送,MAILMESSAGE存储您的电子邮件凭据本地通过自身事先运行这个简短的脚本:

Get-Credential | Export-Clixml C:\fso\myemailcreds.xml 

它会提示您输入您的凭证,然后将其存储在本地(在本例中)名为fso的文件夹中。然后,在任何发送-MAILMESSAGE cmdlet将使用本地存储的凭据,如下所示:

Send-MailMessage -To 'Recipient <[email protected]>' -from 'John Donnelly <[email protected]>' 
    -cc 'whoever <[email protected]>' -Subject $subject -Body $body 
    -BodyAsHtml -smtpserver "smtp.office365.com" -usessl 
    -Credential (Import-Clixml C:\fso\myemailcreds.xml) -Port 587 
+0

感谢约翰,但它仍然无法与我自己的脚本改正我的问题。邮件仍然不会出去 –

+0

您可以发布整个脚本吗? –

+0

约翰,下面是脚本: –