我已经尝试调试这几天了,而且我的智慧结束了。这是代码:
$gmailPwd = 'password'
$gmailUser = 'my.email@gmail.com'
$cred = New-Object System.Management.Automation.PSCredential ($gmailUser,$gmailPwd)
$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential = $cred
From = $gmailUser
To = $gmailUser
Subject = 'Test'
Body = "Test"
}
Send-MailMessage @param
我不断收到以下错误消息:
New-Object : Cannot convert argument "1", with value: "Password1234", for "PSCredential" to type "System.Security.SecureString": "Cannot convert the "Password1234" value of type "System.String" to type
"System.Security.SecureString"."
At line:4 char:9
+ $cred = New-Object System.Management.Automation.PSCredential ($gmailUser,$gmailP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Send-MailMessage : 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:15 char:1
+ Send-MailMessage @param
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
我只是不确定错误信息的来源。并且,为了记录,没有Password1234真的不是密码,但它类似于真实的东西。
答案 0 :(得分:3)
[PSCredential]
的构造函数不接受纯文本密码。它必须是一个安全的字符串。
来自How to create a PSCredential object:
$secpasswd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)