使用 UseSsl 仍有问题。
function SendMailByPowerShell() {
param(
$To=$null,
$Cc=$null,
$Bcc=$null,
$Subject=$null,
$Body=$null,
$Attachments=$null
)
$From = "username"
$FromPwd = "password"
$Port = "25"
$SmtpServer = "smtp.xxxx.xx"
$Priority = "High" # Normal, Low, High
$Encoding = "UTF8" # ASCII,UTF8,UTF7,UTF32,Unicode,BigEndianUnicode,Default,OEM
$NotiOption = "None" # None, OnSuccess, OnFailure, Delay, Never
$OtherOption = "-BodyAsHtml" # -UseSsl -BodyAsHtml
$SecPwd = ConvertTo-SecureString $FromPwd -AsPlainText -Force
$MyCreds = New-Object System.Management.Automation.PSCredential ($From, $SecPwd)
$command = [string]::Concat('Send-MailMessage -From $From -Port $Port -SmtpServer $SmtpServer -Credential $MyCreds ',$OtherOption
,' -Priority $Priority -Encoding $Encoding -DeliveryNotificationOption $NotiOption -To $To -Subject $Subject -Body $Body '
,$(if ($Cc) {" -Cc $Cc"}),$(if ($Bcc) {" -Bcc $Bcc"}),$(if ($Attachments) {" -Attachments $Attachments"}))
try {
Invoke-Expression $command
}
catch [System.Exception] {
"Failed to send email: {0}" -f $_.Exception.Message
}
}
# 邮件发送
$To = ""
$Cc = ""
$Bcc = ""
$Subject = "主题"
$Body = "发送内容"
$Attachments = ""
SendMailByPowerShell $To $Cc $Bcc $Subject $Body $Attachments
SendMailByPowerShell -To $To -Cc $Cc -Bcc $Bcc -Subject $Subject -Body $Body -Attachments $Attachments