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

:)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

范桂飓

文章对您有帮助就请一键三连:)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值