Powershell 邮件发送

目录

前言

最近领导想在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:\pwd\password.txt"
$attach = "E:\attach\test.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:\attach\hd.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

:)

上一篇:.net 发送邮件


下一篇:Python 发送邮件