Powershell - Outlook - 将多个附件添加到电子邮件

标签 powershell email outlook attachment

我想在一封电子邮件中添加多个附件。 一个没问题,但如果您尝试添加两个或更多,就会出现问题

我的代码

$file_patch=Get-ChildItem 'C:\OUTLOOK' | Sort {$_.LastWriteTime} | select -last 1 | % { $_.FullName }
$name=Select-String -Path $file_patch -pattern name
$email=Select-String -Path $file_patch -pattern email
$subject=Select-String -Path $file_patch -pattern subject
$attachment=Select-String -Path $file_patch -pattern attachment
$Signature = Get-Content ($env:USERPROFILE + "\AppData\Roaming\Microsoft\Signatures\*.htm")
$rname = $name -replace ".*:"
$remail = $email -replace ".*:"
$rsubject = $subject -replace ".*:"
$rattachment = $attachment -replace ".*attachment:"
$sname = $rname -split ";"
$semail = $remail -split ";"
$ssubject = $rsubject -split ";"
$sattachment = $rattachment -split ";"
$body=Get-Content C:\OUTLOOK\BODY\$sname.txt
$Signature = Get-Content ($env:USERPROFILE + "\AppData\Roaming\Microsoft\Signatures\*.htm")
$sRecipientAddr = $semail
$sMsgSubject = $ssubject
$oOutlook = New-Object -ComObject Outlook.Application 
$oMapiNs = $oOutlook.GetNameSpace("MAPI")
$oMailMsg = $oOutlook.CreateItem(0)
$oMailMsg.GetInspector.Activate()
$sSignature = $oMailMsg.HTMLBody
[Void]$oMailMsg.Recipients.Add($sRecipientAddr)
$oMailMsg.Attachments.Add($sattachment)
$oMailMsg.Subject = $sMsgSubject
$oMailMsg.HTMLBody = $body + $sSignature

我的文件

名称:展望
电子邮件:[email protected] ; [email protected] ; [email protected]
主题:你很棒
附件:"C:\outlook\attachment\sell.txt";"C:\outlook\attachment\out.txt"

错误:

PS > Value does not fall within the expected range.
+ $oMailMsg.Attachments.Add($sattachment)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], A
+ FullyQualifiedErrorId : System.ArgumentException

可能出了什么问题

最佳答案

您正在尝试将数组直接传递到.attachments.add()page here具有 Add 方法的用法。

因此,我认为如果您以稍微不同的方式添加附件,您应该会成功:

...
$sSignature = $oMailMsg.HTMLBody
[Void]$oMailMsg.Recipients.Add($sRecipientAddr)
$sattachment | ForEach-Object { $oMailMsg.Attachments.Add($_) }
$oMailMsg.Subject = $sMsgSubject
$oMailMsg.HTMLBody = $body + $sSignature

假设 $sattachment = $rattachment -split ";" 实际上返回一个字符串数组,您可以使用 ForEach-Object cmdlet 对其进行循环。然后将为每个数组元素调用 .Add() 方法,该元素在 block 内由 $_ 表示。

关于Powershell - Outlook - 将多个附件添加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42160630/

相关文章:

powershell - 如何使用 PowerShell 检查数组元素是否与字符串匹配?

powershell - 通过 PowerShell 检查页面重定向

ruby-on-rails - ActionMailer 在开发中不发送邮件

如果帐户有多个邮箱,则 VBA 选择邮箱

windows - 在多个设备上同步 Outlook 提醒

powershell - 将过程信息转换为人类可读的值

Php mail() 使用表从数据库发送数据

vba - 如何在没有 Outlook 的情况下通过 VBA 发送电子邮件

python - win32com 解析 Outlook 2010 时显示实例,而不是文件夹/消息的名称/主题

Powershell 哈希表检索问题