2017-09-03 116 views
1

新的論壇,所以如果這在錯誤的領域表示歉意。Powershell自動電子郵件

我希望創造一個PowerShell腳本哪些電子郵件由200個郵件列表文件(.txt),文本文件將藏漢新的電子郵件地址進行更新。將要發送的電子郵件正文是一個標準(通用)電子郵件,並不針對每個電子郵件地址。

這可能嗎?

這是劇本我到目前爲止,但它不是發送電子郵件..提前 馬特

$emails = @() 
     ForEach ($recipient in Get-Content "\\FILE Location") 

     { 
     $emails += "$recipient" 
     } 

$to = "TO EMAIL" 
$smtp = "SMTP Server" 
$from = "FROM EMAIL" 
$subject = "Subject" 
$body = "EMAIL BODY" 
send-MailMessage -SmtpServer $smtp -To $to -Bcc $emails -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high 

感謝。

+1

歡迎來到StackOverflow。如果你有一個你需要解決的特定代碼問題,並且有一些你寫的代碼,你正在努力工作,你通常會得到更好的幫助。查看幫助/入門頁面和「如何問一個好問題」指南。關於你的問題,你想看看'Get-Content'和'Send-MailMessage'的結合。 –

+1

的可能的複製[Powershell的發送-MAILMESSAGE - 電子郵件給多個收件人(https://stackoverflow.com/questions/10241816/powershell-send-mailmessage-email-to-multiple-recipients) – LotPings

+0

這是我到目前爲止... $電子郵件= @() 的ForEach($中的收件人獲取內容 「\\文件位置」) {$ 電子郵件+ = 「$收件人」 } $到= 「到EMAIL」 $ SMTP = 「SMTP服務器」 從$ = 「從電子郵件中」 $主題= 「主題」 $身體= 「EMAIL BODY」 發-MAILMESSAGE -SmtpServer $ SMTP - 要$來-Bcc $從-subject $主題 - 身體$身體電子郵件 - 從$ -BodyAsHtml -Priority高 – MattG

回答

0

在這種情況下,它可能是最好的自己創建的對象。我會這樣設置:

$smtpServer = "SMTP Server" 
$smtp = new-object Net.Mail.SmtpClient($smtpServer) 
$smtpCredentialsUsername = "[email protected]" 
$smtpCredentialsPassword = ConvertTo-SecureString -String $smtpPwd -AsPlainText -Force 

$Credentials = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList 
$smtpCredentialsUsername, $smtpCredentialsPassword 
$smtp.Credentials = $Credentials 

$msg = new-object Net.Mail.MailMessage 
$msg.Body = $body 
$msg.Subject = $subject 
$msg.From = $from 
$msg.To.ADD($to) 
$msg.IsBodyHtml = $true 


ForEach ($recipient in Get-Content "\\FILE Location") 
    { 
    $msg.Bcc.ADD($recipient) 
    } 

$smtp.Send($msg) 

$msg.Dispose();