【问题标题】:Powershell Script - Send Authenticated emailPowershell 脚本 - 发送经过身份验证的电子邮件
【发布时间】:2020-08-04 05:12:57
【问题描述】:

所以我有一个正在运行的报告脚本,我正在向它添加以下脚本,以便它发送电子邮件。除了身份验证失败之外,这一切都有效。

密码存储在一个有效的文件中,并且身份验证部分适用于 -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

【问题讨论】:

  • 这是asked before?您连接到哪个 SMTP 服务器?本地交换?办公365?还有什么?无论如何,为什么不使用Send-MailMessage

标签: powershell scripting powershell-2.0


【解决方案1】:

所以我添加了 Send-MailMessage 而不是从顶部拉出并使其正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 2017-09-26
    • 2016-11-17
    • 2018-08-07
    • 1970-01-01
    相关资源
    最近更新 更多