无法根据需要设置发送电子邮件功能

时间:2018-02-02 12:51:43

标签: powershell outlook

我尝试在脚本中创建Send-Email函数,该函数可以在每次使用脚本时更改电子邮件地址。但是,我现在很难被迫使用静态电子邮件:

function Send-Email (
    $recipientEmail = "EMAIL",
    $subject = "Ticket" + $type,
    $body
) {
    $outlook = New-Object -ComObject  Outlook.Application
    $mail = $outlook.CreateItem(0)
    $mail.Recipients.Add("EMAIL")
    $mail.Subject = ("Ticket - " + $type)
    $mail.Body = $body   # For HTML encoded emails
    $mail.Send()
    Write-Host "Email sent!"
}

我想进行设置,以便我可以定义$recipientEmail,然后将此输入与$mail.Recipients.Add("EMAIL")一起使用 - 我正在做$mail.Recipients.Add("$receipientemail")和喜欢没有任何运气,并想知道我是否接近这个完全错误?

我希望以可以使用

的方式进行设置
[void] $listBox.Items.Add("Email1")
[void] $listBox.Items.Add("Email2")
[void] $listBox.Items.Add("Email3")

接受发送电子邮件,而不是仅使用一封电子邮件。

1 个答案:

答案 0 :(得分:2)

整个事情跟我敲响了一声,然后我去了#dump;编码垃圾箱潜水"。一年前我写了同样的功能(或多或少)。我没有使用$mail.recipients.add();我使用$Mail.To = $RecipientEmail;如果我想要多个收件人,我只需-join ";"他们并分配给$mail.To。成员$mail.CC$mail.BCC的工作方式相同:

Function Send-Email { 
<#
.Synopsis
    Sends Email using Microsoft Outlook
.DESCRIPTION
    This cmdlet sends Email using the Outlook component of a locally-installed
    Microsoft Office. It is not expected to work with the "Click-to-run" versions.
    This version requires that Outlook be running before using the cmdlet.
.PARAMETER To
    Must be a quoted string or string array, may not be omitted.
    Specifies the address to send the message to. If an array, it will
    be converted to a semicolon-delimited string. May contain contact 
    groups.
.PARAMETER Subject
    Must be a quoted string, may not be omitted.
    Specifies the subject of the message to be sent.
.PARAMETER Body
    Must be a string or a string array, may not be omitted.
    Contains the text of the message to be sent. If supplied as a string in double 
    quotes, any PowerShell variables will be expanded unless the leading $ is 
    escaped. If supplied in a script via a variable containing a here-doc, will 
    reproduce the here-doc. If supplied as an array, either by variable or by the 
    Get-Content cmdlet, the array elements will be joined with `r`n as "separators". 
    A body supplied via Get-Content will not have expanded variables; arrays created 
    in scripts using double quotes or via here-docs willl have expanded variables.
.PARAMETER CC
    Must be a quoted string or string array, may be omitted.
    Specifies an address to send a copy of the message to. If an array, it will be
    converted to a semicolon-delimited string. All recipients can see that a copy 
    of the message was sent to the addresses here.
.PARAMETER BCC
    Must be a quoted string or string array, may be omitted.
    Specifies an address to send a copy of the message to. If an array, it will be 
    converted to a semicolon-delimited string. The recipients named here are not 
    displayed as a recipient of the message, even though they do receive the message.
.PARAMETER Attachments
    Must be a quoted string or string array, may be omitted.
    Specifies one or more files to be included with the message as attachments.
.INPUTS
   This version of the cmdlet does not accept input from the pipeline
.OUTPUTS
   No output to the pipeline is generated. The cmdlet does return an error code.
.EXAMPLE
    Send-Email -To "boss@example.com" -Subject "Personnel Issue - John M. Ployee" -Body "I need a raise."

        Sends an email with the indicated subject and body to the stated address.
.EXAMPLE
    $messagebody = @"
    Roses are red
    Violets are blue
    I need a raise
    And so do you
    "@

    $others = "boss-of-boss@example.com","hr@example.com"

    Send-Email -To "boss@example.com" -Subject "Personnel Issue - John M. Ployee" -cc $others -Body $messagebody

        Sends an email with the indicated subject to the stated address. The body 
        will be composed of the lines in the variable $messagebody as shown; the 
        line breaks are preserved. A copy of the message is sent to the two addresses
        listed in $others.
.EXAMPLE
    Send-Email -To "boss@example.com" -Subject "Personnel Issue - John M. Ployee" -Body (Get-Content -Path "C:\Request-for-raise.txt")

        Sends an email with the indicated subject and body to the stated address.
        The body is drawn from the indicated file, and line breaks are preserved.
.EXAMPLE
    Send-Email -To "boss@example.com" -Subject "Personnel Issue - John M. Ployee" -Body "Please see attached for rationale for raise" -Attachments (Get-Content -Path "C:\Request-for-raise.txt")

        Sends an email with the indicated subject and body to the stated address.
        The indicated file is included as an attachment.
.NOTES
    Planned Future Enhancements:
        1. Allow the cmdlet to accept input (message body text) from the pipe.
        2. Allow the cmdlet to accept the body from a file (parameter -BodyFromFile)
        3. Allow the cmdlet to start up Outlook if it is not already running.
           This includes shutting it down afterward.
        4. Allow the body to be formatted as HTML or RTF, and properly handle this.
           Initially, switches (e.g., -BodyAsHTML or -BodyAsRTF); better would be to
           use file name (e.g., -BodyFromFile "C:\Test.html" would still set the message
           format to HTML even in the absence of -BodyAsHTML); best would be to inspect
           content of file.

        Based on a script from http://www.computerperformance.co.uk/powershell/powershell_function_send_email.htm
#>

    [cmdletbinding()]
    Param (
        [Parameter(Mandatory=$True)]
        [String[]]$To,

        [String[]]$CC,

        [String[]]$BCC,

        [Parameter(Mandatory=$True)]
        [String]$Subject,

        [Parameter(Mandatory=$True)]
        [String[]]$Body,

        [String[]]$Attachments
    )

    Begin {
        # Future Enhancement: Start Outlook if it's not already running
    } # End of initialization

    Process {
        # Create an instance Microsoft Outlook
        $Outlook = New-Object -ComObject Outlook.Application
        # Create a new mail message
        $Mail = $Outlook.CreateItem(0)
        # Set up the email
        $Mail.To = $To -join ";"
        if ($CC -ne $null) {
            $Mail.CC = $CC -join ";"
        }
        if ($bcc -ne $null) {
            $Mail.BCC = $BCC -join ";"
        }
        $Mail.Subject = "$Subject"
        $Mail.Body = $Body -join "`r`n"
        if ($attachments -ne $null) {
            foreach ($Attachment in $Attachments) {
                $silencer = $Mail.Attachments.Add($Attachment)
            }
        }
        # I need to investigate scripting Outlook more.
        # The next three comments may be all that's needed
        #   to handle HTML and/or attachments.

        # $Mail.HTMLBody = "When is swimming?"
        # $File = "D:\CP\timetable.pdf"
        # $Mail.Attachments.Add($File)

        # Place message in outbox.
        $Mail.Send()
    } # End of Process section

    End {
        # Clean up after ourselves. This prevents Outlook from reporting
        #   an error on exit.
        [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)
        $Outlook = $null

        # If Outlook settings are to Send/Receive only on command rather
        #   than as-needed, force the send at this time.
        # NOTE TO SELF: CHECK THIS!
        # $Outlook.GetNameSpace("MAPI").SendAndReceive(1) 

        # If we launched Outlook at the beginning, close it here.
        # $Outlook.Quit()
    } # End of End section!
} # End of function
<#

Note 2: Look in Outlook's outbox for a newly-created message.  

Note 3: If you really want to send the email then append this command:
$Outlook.GetNameSpace("MAPI").SendAndReceive(1) 
#>