Powershell 如何使用此代码发送包含多个附件的电子邮件

Powershell 如何使用此代码发送包含多个附件的电子邮件,powershell,Powershell,我对PowerShell了解不多,但我在Stack Overflow中找到了关于如何发送带有附件的电子邮件的代码。我想知道是否有可能改变它一点,这样我可以发送两个附件,而不是一个。我想在不使用ZIP或RAR压缩的情况下发送文件: 附件: “C:\Users\ricar\Desktop\impressora.txt” “C:\Users\ricar\Desktop\impressora2.txt” 我将使用[string[]为-attachmentpath参数使用字符串数组。您还可以省去创建单独的

我对PowerShell了解不多,但我在Stack Overflow中找到了关于如何发送带有附件的电子邮件的代码。我想知道是否有可能改变它一点,这样我可以发送两个附件,而不是一个。我想在不使用ZIP或RAR压缩的情况下发送文件:

附件: “C:\Users\ricar\Desktop\impressora.txt” “C:\Users\ricar\Desktop\impressora2.txt”


我将使用
[string[]
-attachmentpath
参数使用字符串数组。您还可以省去创建单独的
Net.Mail.Attachment
对象的步骤,因为该对象已经包含在您已有的基本
Net.Mail.MailMessage
对象中

例如:

$Username = "myemail@sapo.pt"
$Password = "mypassword"
$path = "C:\Users\ricar\Desktop\impressora.txt","C:\Users\ricar\Desktop\impressora2.txt"

function Send-ToEmail([string]$email, [string[]]$attachmentpath){

    $message = new-object Net.Mail.MailMessage
    $message.From = "myemail@sapo.pt"
    $message.To.Add($email)
    $message.Subject = "Hello how are you"
    $message.Body = "Is this really going to happen?????"
    $attachmentpath | foreach {$message.Attachments.Add($_)}

    $smtp = new-object Net.Mail.SmtpClient("smtp.sapo.pt", "587")
    $smtp.EnableSSL = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
    $smtp.send($message)
    write-host "Mail Sent"  
 }
Send-ToEmail  -email "myfriend@yahoo.com.br" -attachmentpath $path
此外,无需在PowerShell中的每行末尾包含分号。我只会在符合当前代码风格准则的情况下使用它们

$Username = "myemail@sapo.pt"
$Password = "mypassword"
$path = "C:\Users\ricar\Desktop\impressora.txt","C:\Users\ricar\Desktop\impressora2.txt"

function Send-ToEmail([string]$email, [string[]]$attachmentpath){

    $message = new-object Net.Mail.MailMessage
    $message.From = "myemail@sapo.pt"
    $message.To.Add($email)
    $message.Subject = "Hello how are you"
    $message.Body = "Is this really going to happen?????"
    $attachmentpath | foreach {$message.Attachments.Add($_)}

    $smtp = new-object Net.Mail.SmtpClient("smtp.sapo.pt", "587")
    $smtp.EnableSSL = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
    $smtp.send($message)
    write-host "Mail Sent"  
 }
Send-ToEmail  -email "myfriend@yahoo.com.br" -attachmentpath $path