powershell - emailmessage - 附加文件类型的每个文件

powershell - emailmessage - attach every file of a filetype

我正在尝试将特定文件类型的每个文件作为附件通过电子邮件发送。如果我有
单个 pdf 脚本有效。但是,很多都是一天创建的。当任务计划程序运行时,我的脚本不会发送文件夹中的每个 pdf。它只会接收和发送一个文件。这是脚本的一部分。我也试过“foreach($sourcedir 中的 $createdproj)

$sourcedir = 'E:\projectFolder'   
$createdproj = Get-Item -path 'E:\ProjectFolder\*.pdf' 

if ($createdproj ) {
$emailSmtpServer = "smtp.domain.com"
$emailSmtpServerPort = "25"
$emailSmtpUser = "user@domain.com"

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "admin@domain.com"
$emailMessage.To.Add( "support@IT-Help.com" )
$emailMessage.Subject = "Project Successfully Created"
$emailMessage.Attachments.Add($createdproj)
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = @"
<p>Here is a message states the csv was converted to pdf</p>
"@

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer ,        $emailSmtpServerPort )

$SMTPClient.Send( $emailMessage )

}

I have also tried "foreach ($createdproj in $sourcedir)

通过 $sourcedir 进行 Foreaching 不会获得目录中的所有项目。您应该知道这一点,因为在下一行您将获得目录中的文件。

按原样使用您的代码,您应该能够

foreach ($proj in $createdproj) {
  $emailMessage.Attachments.Add($proj)
}

谢谢。那奏效了。这是决赛:

foreach ($proj in $createdproj) {

$emailSmtpServer = "smtp.client.com"
$emailSmtpServerPort = "25"
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "system@client.com"
$emailMessage.To.Add( "support@ITcompany.com" )
$emailMessage.Subject = "Project Successfully Created"
$emailMessage.Attachments.Add($sourcedir + $proj.name)

$emailMessage.IsBodyHtml = $true
$emailMessage.Body = @"
<p>A CSV has been created and processed with information containing project    . </p>
"@

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer ,     $emailSmtpServerPort )

$SMTPClient.Send( $emailMessage )

}