使用 powershell 发送电子邮件

Tib*_*ibi 2 email powershell

我想用powershell发送电子邮件。如果我手动输入我的凭据,该脚本可以正常工作。但我想在脚本中提供凭证参数。我的脚本如下所示:

$From = "test@yahoo.de"
$To = "test2@yahoo.de"
$Cc = "test@yahoo.de"
$Attachment = "C:\Users\test\test\test.ini"
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = "smtp.mail.yahoo.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential ( 
$MyClearTextUsername=’test@yahoo.de’
$MyClearTextPassword=’test123’

$SecurePassword=Convertto-SecureString –String $MyClearTextPassword –AsPlainText –force

$MyCredentials=New-object System.Management.Automation.PSCredential $MyClearTextPassword,$SecurePassword) -Attachments $Attachment
Run Code Online (Sandbox Code Playgroud)

4c7*_*b41 5

以下是创建凭证对象的方法:

$cred = ([pscredential]::new('test@yahoo.de',(ConvertTo-SecureString -String 'test123' -AsPlainText -Force)))
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下使用:

Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
  -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
  -Credential $cred -Attachments $Attachment
Run Code Online (Sandbox Code Playgroud)

我认为尝试将其放入Send-MailMessage. 更容易阅读。