powershell 发邮件

参考文章:https://www.cnblogs.com/lsgxeva/p/9309217.html

文章内容:

问:如何用powershell发邮件?
答:
Send-MailMessage   -Subject "主题"       `
-From  "你的hotmail账户@hotmail.com"   -To  "你的qq邮箱@qq.com"   `
-SmtpServer   "smtp.live.com"  -Port 587  -UseSsl    -Credential  "你的hotmail账户@hotmail.com"  `
-Attachments $附件 -BodyAsHTML -body $HTML邮件内容

注意:
1 用xxx@QQ.com + 你的qq密码 + 上述命令 发邮件是不行的。因为为了qq密码安全,腾讯要求独立的邮箱密码。  
2 从qq邮箱发件,默认也不行。被关了,需要在设置中开启smtp。
3 powershell 3.0 和 以上 才支持port参数。对于win7,你需要先安装ps最新版

 

我的代码:

# 简单版
Send-MailMessage -Subject "powershell send Email" ` -From "taozXX@hotmail.com" -To "3845XX@qq.com" ` -SmtpServer "smtp.live.com" -Port 587 -UseSsl -Credential "taozXX@hotmail.com" ` -BodyAsHTML -body "powershell send Email from taozXX@hotmail.com to 3845XX@qq.com" #弹出密码框,填写hotmail密码
#-SmtpServer   "smtp.office365.com"  -Port 587  -UseSsl    -Credential  "taozXXX@hotmail.com" `

 

# 函数版,并且加了安全机制
# Write-Output 不要随便用,会有管道传递,Write-Host功能少,只是简单打印

function SendEmail {
    param (
        $SmtpServer = 'smtp.office365.com', # This is the address of the Office 365 SMTP server we will be using.
        $SmtpPort = '587', #This is the Port of the Office 365 SMTP server we will be using.
        $SmtpUser = 'taoXXX@hotmail.com', # Specify your Office 365 User ID
        #$SmtpPassword = '', # Specify your Office 365 password, but not secure, more information belower 
        $SmtpSecureFile = 'secure.file', # Specify your password secure.file, more secure
        $MailFrom = 'taoXXX@hotmail.com', # Specify the Source Email ID
        [Parameter(Mandatory=$true)]$MailTo, # Specify the users to which the mails should be sent. $MailTo = 't-shtao@microsoft.com'
        [Parameter(Mandatory=$true)]$MailSubject, # This is the Mail Subject line. $MailSubject = "powershell send Email by t-shtao"
        $MailBody, # This is the Mail body. $MailBody = "Hello <span style='font-weight:bold;'>Microsoft</span>.",
        $MailAttachments # This is the Mail attachments' file path. $MailAttachments = "C:\PBID-qs\attachments\attachment01.txt"
    )
    # get Email secure password by [SecureString]
    [String]$SmtpPassword = SecurefileToString -SecureFile $SmtpSecureFile
    # make credentials by Email secure password
    $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -AsPlainText -Force) 
    # MailParameters setting
    $MailParameters = @{
        Subject=$MailSubject
        From=$MailFrom
        To=$MailTo
        SmtpServer=$SmtpServer
        Port=$SmtpPort
        UseSsl=$true
        Credential=$Credentials
        BodyAsHtml=$true
    }
    # add html body
    if ($MailBody) { $MailParameters.Add('Body',$MailBody) }
    # add attachments
    if ($MailAttachments) { $MailParameters.Add('Attachments',$MailAttachments) }
    # send Email
    Send-MailMessage @MailParameters
    # send successfully. Write some information
    Write-Host "Send Successfully. SmtpServer:$SmtpServer, From:$MailFrom, To:$MailTo"
}


function StringToSecurefile {
    # [string]password convert to secure.file
    param (
        $OrdinaryString, # the string need secure
        $SecureFile = 'secure.file' # storeage of secure string
    )
    # string convert to file
    $OrdinaryString | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File -FilePath $SecureFile
    Write-Host "password has converted to $SecureFile"
}

function SecurefileToString {
    # secure.file convert to [string]password
    param (
        $SecureFile = 'secure.file' # storeage of secure string
    )
    # file convert to secure string
    $secureString = Get-Content -Path $SecureFile | ConvertTo-SecureString
    $ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($secureString)
    $OrdinaryString = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr) 
    Write-Host "password has read"
    return $OrdinaryString
}


# make password's secure.file by visible password string. run only first 
# StringToSecurefile -OrdinaryString "password" -SecureFile "secure.file"

# send Email, [Mandatoty]MailTo, [Mandatory]MailSubject, MailBody, MailAttachments
SendEmail -MailTo "tXXX@microsoft.com" -MailSubject "powershell send Email" `
    -MailBody "Hello <span style='font-weight:bold;'>Microsoft</span>." `
    -MailAttachments "C:\PBID-qs\attachments\attachment01.txt"

 

 

 

posted @ 2018-12-18 16:26  fdu_taoshiqian  阅读(1216)  评论(0编辑  收藏  举报