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: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
:)
相關文章
- 郵件傳送
- 傳送郵件
- SpringBoot整合Mail傳送郵件&傳送模板郵件Spring BootAI
- Laravel 傳送郵件Laravel
- PHP傳送郵件PHP
- Django——郵件傳送Django
- java郵件傳送Java
- Laravel傳送郵件Laravel
- gmail傳送郵件AI
- Oracle郵件傳送Oracle
- java傳送郵件Java
- thinkphp 郵件傳送PHP
- centos 傳送郵件CentOS
- phpcms傳送郵件PHP
- 郵件的傳送
- Java Mail 郵件傳送(二):簡單封裝的郵件傳送JavaAI封裝
- golang傳送郵件(qq郵箱)Golang
- 郵件傳送API整理API
- php windows 傳送郵件PHPWindows
- 定時傳送郵件
- thinkjs 傳送郵件JS
- 使用phpmailer傳送郵件PHPAI
- Ubuntu的郵件傳送Ubuntu
- python傳送郵件Python
- nodejs 傳送郵件NodeJS
- 傳送郵件那些事
- javamail郵件傳送例子JavaAI
- 使用JavaMail傳送郵件JavaAI
- 使用nodemailer傳送郵件AI
- javamail傳送郵件(轉)JavaAI
- 使用python傳送郵件和接收郵件Python
- 如何傳送電子郵件到別人郵箱?電子郵件傳送的方法
- 使用 smtplib 傳送郵件
- Laravel 傳送郵件報錯Laravel
- Android 快速傳送郵件Android
- node 定時傳送郵件
- Laravel 佇列傳送郵件Laravel佇列
- 如何使用Excel傳送郵件?Excel