[英]How to send an email from Powershell script without authentication?
我正在嘗試使用 Powershell 自動發送 email 而無需進行身份驗證,因為我使用的 From id 是服務帳戶並且不需要密碼。
下面是我的 Powershell 代碼
$smtpServer = "<SMTP Server>"
$smtpFrom = "<from email id - this is a service account and has no password>"
$smtpTo = "<to email id>"
$messageSubject = "Test"
$messageBody = "Test"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)
當我執行此 powershell 腳本時,我收到以下錯誤
Exception calling "Send" with "4" argument(s): "Failure sending mail."
At <Script_Locaion>\email1.ps1:13 char:1
+ $smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException
任何指針?
提前致謝。
您需要檢查郵件服務器日志以找出郵件未被接受的原因。
順便說一句:您還可以使用內置的 Send-MailMessage 命令,它使用相同的庫。 但是,System.Net.Mail 和 Send-MailMessage 命令都被 Microsoft 標記為已過時,他們建議不再使用它。
我使用New-Object System.Net.Mail.MailMessage
但我的 SMTP 服務器配置為從本地網絡發送而無需密碼(僅當用戶名和郵箱存在時)。
$Encoding = [System.Text.Encoding]::UTF8
$MailFromSup = "from@mail.com"
$MailToSup = "to@mail.com"
$MailSubj = "SUBJECT"
$MailTextSup = "MAIL BODY"
$MailSMTPServer = "ourserver.mail.com"
$MailSMTPPort = "25"
$SMTPMessageSup = New-Object System.Net.Mail.MailMessage $MailFromSup, $MailToSup, $MailSubj, $MailTextSup
$SMTPMessageSup.BodyEncoding = $Encoding
$SMTPMessageSup.SubjectEncoding = $Encoding
$SMTPClient = New-Object net.mail.smtpclient($MailSMTPServer,$MailSMTPPort)
$SMTPClient.Send($SMTPMessageSup)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.