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
- 郵件傳送
- 傳送郵件
- java郵件傳送Java
- python傳送郵件Python
- Django——郵件傳送Django
- gmail傳送郵件AI
- phpcms傳送郵件PHP
- 郵件的傳送
- Laravel 傳送郵件Laravel
- thinkjs 傳送郵件JS
- SpringBoot傳送郵件Spring Boot
- SpringBoot郵件傳送Spring Boot
- Laravel傳送郵件Laravel
- PHP傳送郵件PHP
- nodejs 傳送郵件NodeJS
- Java Mail 郵件傳送(二):簡單封裝的郵件傳送JavaAI封裝
- golang傳送郵件(qq郵箱)Golang
- Android 快速傳送郵件Android
- 使用phpmailer傳送郵件PHPAI
- SpringBoot傳送郵件(二)Spring Boot
- SpringBoot傳送郵件(三)Spring Boot
- 定時傳送郵件
- 使用 smtplib 傳送郵件
- Python SMTP傳送郵件Python
- 郵件傳送API整理API
- php windows 傳送郵件PHPWindows
- SpringBoot整合郵件傳送Spring Boot
- Ubuntu的郵件傳送Ubuntu
- 如何傳送電子郵件到別人郵箱?電子郵件傳送的方法
- node 定時傳送郵件
- Laravel 佇列傳送郵件Laravel佇列
- Spring Boot整合郵件傳送Spring Boot
- python實現傳送郵件Python
- Springboot 自動傳送郵件Spring Boot
- Laravel 傳送郵件報錯Laravel
- Jenkins 如何成功傳送郵件?Jenkins
- 郵件和簡訊傳送
- 命令列郵件傳送工具命令列