Powershell - Outlook - 将多个附件添加到电子邮件
问题描述:
我想向电子邮件添加多个附件。 有了一个没有问题,但如果你想添加两个或更多的不顺心的事Powershell - Outlook - 将多个附件添加到电子邮件
我的代码
$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
我的文件
名称:展望
电子邮件:电子邮件@免得。 pl; [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()
方法,该数组元素在块内由$_
表示。