zoukankan      html  css  js  c++  java
  • 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-qsattachmentsattachment01.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-qsattachmentsattachment01.txt"
    
    
    
     
  • 相关阅读:
    关于w3wp进程占用过多cpu的问题
    调试事务时的小坑
    PowerDesign中的Reverse Engineering
    对数据访问层的重构(及重构中Perl的应用)
    请教关于在asp.net站点中使用静态变量的问题
    .net面试应知应会(zt)
    关于代码运行效率问题的一个总结和一点疑问
    自己写的一个使用游标的小例子
    怎样才能写出尽可能让编译器找出潜在错误的代码?
    关于连接字符串中IMEX参数的一个问题
  • 原文地址:https://www.cnblogs.com/taoshiqian/p/10138014.html
Copyright © 2011-2022 走看看