Powershell郵件傳送

範桂颶發表於2016-02-14

目錄

前言

最近領導想在winServer2012上搞個自動傳送郵件的計劃任務,下面有幾種傳送郵件的方式。
如果Powershell V2.0 以上建議使用第一種方式(比較無腦),2.0以下的話也可以使用第二種方法。

Send-MailMessage

Syntax

Send-MailMessage -To $to -From $from -cc $cc -Subject $sub -Body $body -Credential $mycreds -SmtpServer $smtp -DeliveryNotificationOption Never -BodyAsHtml -UseSsl -port 587 -Attachments $attach

Example:

#Create the secure passward
Function Set-SecurePwd($storage)
{

    $mysecret = `YOURPASSWORD`
    $mysecret | 
    ConvertTo-SecureString -AsPlainText -Force |    #將加密的標準字串轉換為安全字串。它還可以將純文字轉換為安全字串。
    ConvertFrom-SecureString |      #將安全字串轉換為加密的標準字串。
    Out-File -FilePath $storage     #將加密的標準字元輸出到指定檔案

    $pw = Get-Content $storage | ConvertTo-SecureString     #獲取經過加密的標準字元並轉換為安全字串

    return $pw
}

Function Send-Email($attach)
{
    $pwd = Set-SecurePwd $storage

    $cred = New-Object System.Management.Automation.PSCredential "userName",$pwd    #建立身份認證物件
    $to = "xxx@xxx.com"
    $from = "xxx@163.com"
    $cc = "xxx@xxx.com"
    $sub = "TEST"
    $body = "Just test"
    $smtp = "smtp.163.com"

    Send-MailMessage -To $to -From $from -cc $cc -Subject $sub -Body $body -Credential $cred -SmtpServer $smtp -UseSsl -port 25 -Attachments $attach 

    if($?)
    {
        Write-Host "Sent Successfully!" -ForegroundColor Green
    }
    else
    {
        Write-Host "Error" -ForegroundColor Red
    }
}


#Main

$storage = "E:pwdpassword.txt"
$attach = "E:attach	est.txt"

Send-Email $attach    #指定需要傳送的附件

注意$from 的Address必須能夠與$cred身份認證物件一致,這個例子使用了163郵件的SMTP Server 。

.NET.Mail

還可以使用.NET支援的例項來實現郵件傳送,上面的Cmdlet也是呼叫了這一例項。

Function send-mail
{ 
    param( 
        [string]$toAddress = $(throw "toAddress must be set") 
        ,[string]$Subject = $(throw "subject must be set") 
        ,[string]$body = "" 
        ,[string]$file = "") 
#mail server configuration 
    $smtpServer = "SMTPSERVERADDRESS" 
    $smtpUser = "smtpUserName" 
    $smtpPassword = "smtpUserPwd" 
    $sslNeed =$true 
#SMTP server needs SSL should set this attribute 
    $MailAddress ="xxx@163.com" 
    $fromName = "mailAccountName" 
    $replyTo = "xxx@163.com" 
#create the mail message 
    $mail = New-Object System.Net.Mail.MailMessage 
#set the addresses 
    $mail.From = New-Object System.Net.Mail.MailAddress($MailAddress,$fromName) 
    $mail.To.Add($toAddress) 
#set the content 
    $mail.Subject = $Subject 
    $mail.Priority = "High" 
    $mail.Body = $Body 
    $filename= $file 
    $attachment = new-Object System.Net.Mail.Attachment($filename) 
    $mail.Attachments.Add($attachment) 
#send the message 
    $smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer 
    $smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword 
    $smtp.EnableSsl = $sslNeed; 
    try{ 
        $smtp.Send($mail) 
        echo `Ok,Send succed!` 
    } 
    catch 
    { 
        echo `Error!Filed!` 
    } 
}
Send-Mail "xxx@xxx.com" "TEST" "Just test" "E:attachhd.txt"

使用OutLook傳送郵件

這是呼叫了本地的MAPI Client程式,不能自動傳送,只是填充了郵件資訊,需要手動點選傳送。例子來自——Powershell中文部落格

$subject = `Sending via MAPI client`
$body = `My Message`
$to = `tobias@powertheshell.com`

$mail = "mailto:$to&subject=$subject&body=$body"

Start-Process -FilePath $mail

:)


相關文章