错误
New-Object:找不到“PSCredential”的重载和 参数计数:“2”。在D:\ Scripts \ gsend.ps1:12 char: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 = "me@gmail.com"
$EncryptedPW = Get-Content "D:\scripts\gsendcred.txt"
$EncryptedCredential = ConvertTo-SecureString -String $EncryptedCredential -
AsPlainText -Force
$Credential = New-Object Management.Automation.PSCredential ($EmailUsername,
$EncryptedPW)
$EmailFrom = "me@gmail.com"
$EmailTo = "me@gmail.com"
$EmailSubject = "GSEND Test Subject"
$EmailBody = "Test Body"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = 587
$SMTPSsl = $true
答案 0 :(得分:1)
我相信您的问题是处理凭据。以下是我处理smtp凭据的方法:
$smtpPwd = "password"
$smtpCredentialsUsername = "jdonnelly@xxxxx.com"
$smtpCredentialsPassword = ConvertTo-SecureString -String $smtpPwd -AsPlainText -Force
$Credentials = New-Object –TypeName System.Management.Automation.PSCredential
–ArgumentList $smtpCredentialsUsername, $smtpCredentialsPassword
然后:
$smtp.Credentials = $Credentials
或者,如果您使用Send-MailMessage,请事先在本地运行此简短脚本来存储您的电子邮件凭据:
Get-Credential | Export-Clixml C:\fso\myemailcreds.xml
它将提示您输入您的凭据,然后将它们存储在本地(在本例中)名为fso的文件夹中。然后在任何Send-MailMessage cmdlet中使用本地存储的凭据,如下所示:
Send-MailMessage -To 'Recipient <recipient@yahoo.com>' -from 'John Donnelly <jdonnelly@xxx.com>'
-cc 'whoever <whoever@xxx.com>' -Subject $subject -Body $body
-BodyAsHtml -smtpserver "smtp.office365.com" -usessl
-Credential (Import-Clixml C:\fso\myemailcreds.xml) -Port 587
答案 1 :(得分:0)
您可以使用以下Powershell脚本发送邮件
Send-MailMessage -To&#34; abc@abc.com" - 来自&#34; def@abc.com" -Cc&#34; gef@abc.com",&#34; hij@abc.com" -Subject Test Mail -BodyAsHtml $ htmlbody -SmtpServer&#34; mailhost.abc.com&#34;
其中$ htmlbody是包含html的字符串变量。