How to attach a file to an email with PowerShell

前端 未结 5 622
广开言路
广开言路 2021-01-17 11:23

I have written a PowerShell script that will create an email, however I can\'t seem to attach a file. The file does exist and PowerShell can open it, Could anyone tell me wh

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-17 12:05

    If you are on PowerShell 2.0, just use the built-in cmdlet Send-MailMessage:

    C:\PS>Send-MailMessage -from "User01 " `
                           -to "User02 ", `
                               "User03 " `
                           -subject "Sending the Attachment" `
                           -body "Forgot to send the attachment. Sending now." `
                           -Attachment "data.csv" -smtpServer smtp.fabrikam.com
    

    If you copy/paste this watch out for the extra space added after the backtick. PowerShell doesn't like it.

提交回复
热议问题