Powershell脚本-发送经过身份验证的电子邮件

时间:2020-04-21 12:43:07

标签: powershell scripting powershell-2.0

因此,我有一个正在运行的报告脚本,并且正在向其中添加以下脚本,以便它发送电子邮件。除了身份验证失败外,这一切都起作用。

密码存储在一个可以正常工作的文件中,并且身份验证部分适用于-Sendmail版本,但是我无法通过身份验证将其添加到脚本中。

感谢您的帮助

我认为这是我需要对“ $ cred”进行身份验证的部分“ IF($ SendEmail -eq $ TRUE){$ smtp.Send($ msg)}”

$msg = new-object Net.Mail.MailMessage
$msg.IsBodyHTML = $TRUE    
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
$AdminName = "alerts@alkait.co.uk"
$Pass = Get-Content "C:\Masters\WIN SERVER BACKUP SCRIPT\cred.txt" | ConvertTo-SecureString
$msg.From = $FromAddress
$msg.To.Add($ToAddress)
$msg.Subject = $ReportSubject
$msg.Body = $sb

# Format the date and time for the file.
$VarDateTime = (Get-Date -format "yyyy-MMMM-dd-dddd-HHmm")
# Note the extension for the $CleanOldReports
$OutPut = "$OutPutPath\$VarDateTime.html"
# The output folder - The parent folder of the $OutPut.
$TARGETDIR = (split-path $OutPut -Parent)
# If the output folder does not exist then create it.
IF (!(Test-Path -Path $TARGETDIR )) {New-Item -ItemType directory -Path $TARGETDIR}
#Output the report to a file.
"$sb" | Out-File "$OutPut"
# If $TRUE then open the backup report in your default web browser.
IF ($ShowReport -eq $TRUE) {"$OutPut" | invoke-expression}
# If $TRUE then email the report to backup operators.
IF ($SendEmail -eq $TRUE) {$smtp.Send($msg)}
Write-Host "Report Successfully Generated." -ForegroundColor green

1 个答案:

答案 0 :(得分:1)

因此,我添加了Send-MailMessage,而不是从顶部开始及其工作。