我正在尝试使用PowerShell脚本发送电子邮件,我公司的病毒扫描(McAfee)阻止了端口25.我知道我可以进入迈克菲的设置并取消选中“阻止群发电子邮件..选项“,我也知道我可以在我的脚本中调用Outlook以在用户登录时发送电子邮件(这太过于干扰)。我的问题是,有没有其他方法使用PowerShell发送电子邮件而不使用我上面提到的任何选项。
谢谢。
$From = "name@name.com"
$To = "name@name.com"
$Body = Test"
$Subject2 = "Hi"
$SMTPServer ="smtp.com"
Send-MailMessage -From $From -To $To -SmtpServer $SMTPServer -Subject $Subject1 -Body $Body'
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "name@name.com"
$Mail.Subject = "Hi"
$Mail.Body = "Hello"[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$Mail.Send() '
答案 0 :(得分:1)
您可以手动创建[MailMessage]对象并使用[SmtpClient]类发送它,但这可能会像Send-MailMessage
那样与McAfee软件发生冲突。
$From = "name@name.com"
$To = "name@name.com"
$Body = "Test"
$Subject2 = "Hi"
$SMTPServer ="smtp.com"
$message = New-Object System.Net.Mail.MailMessage $From, $To
$message.Subject = $Subject2
$message.Body = $Body
$smtpclient = New-Object Net.Mail.SmtpClient($SMTPServer)
$smtpclient.Send($message)
要使用EWS,您需要 Microsoft.Exchange.WebServices.dll 文件,您可以在以下某处找到该文件:
Mike Pfeiffer博客的示例功能实现在这里(并在下面链接):
Function Send-EWSMailMessage {
[CmdletBinding()]
param(
[Parameter(Position=0, ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[Object]
$PrimarySmtpAddress,
[Parameter(Position=1, Mandatory=$true)]
[System.String]
$Subject,
[Parameter(Position=2, Mandatory=$true)]
[System.String]
$Body
)
BEGIN {
Add-Type -Path "C:\bin\Microsoft.Exchange.WebServices.dll"
$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
$user = [ADSI]"LDAP://<SID=$sid>"
}
PROCESS {
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$service.AutodiscoverUrl($user.Properties.mail)
if($PrimarySmtpAddress -is [Microsoft.Exchange.Data.SmtpAddress]) {
$Recipient = $PrimarySmtpAddress.ToString()
} else {
$Recipient = $PrimarySmtpAddress
}
$mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service)
$mail.Subject = $Subject
$mail.Body = $Body
[Void]$mail.ToRecipients.Add($Recipient)
$mail.SendAndSaveCopy()
}
END {
}
}
以下链接可能有用,但并不专门针对PowerShell实施:
答案 1 :(得分:0)
您使用的SMTP服务器是否支持加密连接?如果是这样,试试:
Send-MailMessage -To $to -From $from -SmtpServer $smtp -UseSsl -Port "587"
我会测试它,但我没有迈克菲。