使用“1”个参数调用“发送”时发生 PowerShell 异常:“发送邮件失败。”

问题描述 投票:0回答:3

运行此脚本时遇到以下错误:

使用“1”个参数调用“发送”时发生异常:“发送邮件失败。”

#Variables to configure
$MailServer = "domain.com"
$ReportSender = "[email protected]"
$ReportRecipient = "[email protected]"
$MailSubject = ("yourdomain.local exchange2010 Mailbox Report for " + $MailServer + " - " + ( get-date ).ToString('yyyy/MM/dd'))

#SendEmailFunction
Function send-mailmessage
{ param($smtphost,$htmlFileName)
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $ReportSender, $ReportRecipient, $MailSubject, (Get-Content $htmlFileName)
$msg.isBodyhtml = $true
$smtp.send($msg)
}

$exdata = Get-MailboxStatistics -Server $MailServer | Sort-Object TotalItemSize -descending | Select-Object DisplayName, ItemCount, TotalItemSize, Database, StorageLimitStatus

$fileName = "exchange2010Report.html"
New-Item -ItemType file $fileName -Force

# HTML start
Add-Content $fileName "<html>"

# HEAD start
Add-Content $fileName "<head>"

add-content $fileName '<STYLE TYPE="text/css">'
add-content $fileName  "<!--"
add-content $fileName  "td {"
add-content $fileName  "font-family: Tahoma;"
add-content $fileName  "font-size: 11px;"
add-content $fileName  "border-top: 1px solid #999999;"
add-content $fileName  "border-right: 1px solid #999999;"
add-content $fileName  "border-bottom: 1px solid #999999;"
add-content $fileName  "border-left: 1px solid #999999;"
add-content $fileName  "padding-top: 0px;"
add-content $fileName  "padding-right: 0px;"
add-content $fileName  "padding-bottom: 0px;"
add-content $fileName  "padding-left: 0px;"
add-content $fileName  "}"
add-content $fileName  "body {"
add-content $fileName  "margin-left: 5px;"
add-content $fileName  "margin-top: 5px;"
add-content $fileName  "margin-right: 0px;"
add-content $fileName  "margin-bottom: 10px;"
add-content $fileName  ""
add-content $fileName  "table {"
add-content $fileName  "border: thin solid #000000;"
add-content $fileName  "}"
add-content $fileName  "-->"
add-content $fileName  "</style>"

# HEAD end
Add-Content $fileName "</head>"

# HEAD start
Add-Content $fileName "<body>"

# TABLE start
Add-Content $fileName "<table width='100%'>"

# TABLE Header
Add-Content $fileName "<tr bgcolor='#7C7C7C'>"
Add-Content $fileName "<td width='35%'>DisplayName</td>"
Add-Content $fileName "<td width='10%'>ItemCount</td>"
Add-Content $fileName "<td width='10%'>TotalItemSize</td>"
Add-Content $fileName "<td width='25%'>Database</td>"
Add-Content $fileName "<td width='20%'>StorageLimitStatus</td>"
Add-Content $fileName "</tr>"

$alternateTableRowBackground = 0

# TABLE Content
while($alternateTableRowBackground -le $exdata.length)
{
if(($alternateTableRowBackground % 2) -eq 0)
{
Add-Content $fileName "<tr bgcolor='#CCCCCC'>"
}
else
{
Add-Content $fileName "<tr bgcolor='#FCFCFC'>"
}
Add-Content $fileName ("<td width='30%'>" + $exdata[$alternateTableRowBackground].DisplayName + "</td>") 
Add-Content $fileName ("<td width='10%'>" + $exdata[$alternateTableRowBackground].ItemCount + "</td>")
Add-Content $fileName ("<td width='15%'>" + $exdata[$alternateTableRowBackground].TotalItemSize + "</td>")
Add-Content $fileName ("<td width='25%'>" + $exdata[$alternateTableRowBackground].Database + "</td>")
#BelowLimit or NoChecking
if(($exdata[$alternateTableRowBackground].StorageLimitStatus -eq "BelowLimit") -or ($exdata[$alternateTableRowBackground].StorageLimitStatus -eq "NoChecking"))
{
Add-Content $fileName ("<td bgcolor='#007F00' width='20%'>" + $exdata[$alternateTableRowBackground].StorageLimitStatus + "</td>")
}
#IssueWarning
if($exdata[$alternateTableRowBackground].StorageLimitStatus -eq "IssueWarning")
{
Add-Content $fileName ("<td bgcolor='#7F7F00' width='20%'>" + $exdata[$alternateTableRowBackground].StorageLimitStatus + "</td>")
}
#ProhibitSend or MailboxDisabled
if(($exdata[$alternateTableRowBackground].StorageLimitStatus -eq "ProhibitSend") -or ($exdata[$alternateTableRowBackground].StorageLimitStatus -eq "MailboxDisabled"))
{
Add-Content $fileName ("<td bgcolor='#7F0000' width='20%'>" + $exdata[$alternateTableRowBackground].StorageLimitStatus + "</td>")
}
Add-Content $fileName "</tr>"

$alternateTableRowBackground = $alternateTableRowBackground + 1
}
# Summe Mailboxsize
Add-Content $fileName "<tr bgcolor='#7C7C7C'>"
Add-Content $fileName ("<td width='30%'></td>") 
$tempdata = MailboxStatistics -Server $MailServer | %{$_.ItemCount} | Measure-Object -Sum
Add-Content $fileName ("<td width='10%'>" + ($tempdata | Select-Object -expand Sum) + "</td>")
$tempdata = MailboxStatistics -Server $MailServer | %{$_.TotalItemSize.Value.ToMB()} | Measure-Object -Sum
Add-Content $fileName ("<td width='15%'>" + ($tempdata | Select-Object -expand Sum) + " MB</td>")
Add-Content $fileName ("<td width='25%'></td>")
Add-Content $fileName ("<td width='20%'></td>")


#TABLE end
Add-Content $fileName "</table>"

# HEAD end
Add-Content $fileName "</body>"

# HTML end
Add-Content $fileName "</html>"

send-mailmessage $MailServer $fileName

报告生成正常,文件也生成,但不会发送电子邮件,我收到错误。不知道为什么我会得到它。提前致谢!

email powershell exchange-server
3个回答
1
投票

电子邮件服务器需要身份验证,所以我在发送之前添加了这一行

$SMTP.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "password")

0
投票

有这个

$emailSmtpServer = "mail.somewhere.com"
$emailFrom = "[email protected]"
$emailTo = "[email protected]"
$emailSubject = "$domain report $env:computername subject $date"
$emailBody = "Get-Content c:\exchange2010Report.html"
Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -BodyAsHTML -SmtpServer  $emailSmtpServer 

0
投票

还没有人提到

Smtp*
异常有一个名为
StatusCode

的自定义属性

此处描述了枚举:[Net.Mail.SmtpStatusCode]

记录
SmtpStatusCode

您在哪里收到此错误

使用“1”个参数调用“发送”时发生 PowerShell 异常:“发送邮件失败。”

尝试这样的事情。 (我不确定该 cmdlet 是否需要

-ea

try { 
   Send-MailMessage -ea Stop .....
} catch {
   $_.Exception.StatusCode | Write-Host
   $_.Exception | Write-Host
}

这是一个硬编码测试来验证该属性是否存在

try { 
   throw [System.Net.Mail.SmtpException]::new()
}  catch {
@"
    StatusCode: $( $_.Exception.StatusCode )
    Exception : $( $_.Exception )
"@
}
© www.soinside.com 2019 - 2024. All rights reserved.