所以我得到了一个包含不同 PDF 文件(“RG 330526.pdf”、“RG 330527.pdf”、“RG 330528.pdf”)等的文件夹。
现在,我想创建一封新电子邮件,从文件夹 (C:\1_PDF) 中附加第一个 pdf 文件,并将主题命名为 pdf 文件的名称。将电子邮件保存在草稿中,然后重新开始下一封电子邮件/下一个 pdf 文件。
这是我目前得到的,我需要一些有关循环的帮助:
$Outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.Logon($null, $null, $false, $true);
$EmailFrom = ("[email protected]")
$Mail = $Outlook.CreateItem(0)
$Mail.To = $EmailTo
$Mail.Subject = $EmailSubject
#$signature = $Mail.HtmlBody
#$Mail.HtmlBody = $EmailHtmlBody + $signature
$account = $outlook.Session.Accounts.Item($EmailFrom)
function Invoke-SetProperty {
param(
[__ComObject] $Object,
[String] $Property,
$Value
)
[Void] $Object.GetType().InvokeMember($Property,"SetProperty",$NULL,$Object,$Value)
}
Invoke-SetProperty -Object $mail -Property "SendUsingAccount" -Value $account
#$Mail.GetInspector.Activate()
$Mail.Save()
答案1
搞定了!
这对我有用:
$WorkDir = "C:\1_PDF"
$RGMailList = Get-ChildItem -Path $WorkDir -Filter *.pdf
ForEach ($RGMail in $RGMailList)
{
$Outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.Logon($null, $null, $false, $true);
$EmailFrom = ("[email protected]")
$Mail = $Outlook.CreateItem(0)
$Mail.To = "[email protected]"
$Mail.Subject = "Subject " + "$RGMail".substring(3)
$account = $outlook.Session.Accounts.Item($EmailFrom)
function Invoke-SetProperty {
param(
[__ComObject] $Object,
[String] $Property,
$Value
)
[Void] $Object.GetType().InvokeMember($Property,"SetProperty",$NULL,$Object,$Value)
}
Invoke-SetProperty -Object $mail -Property "SendUsingAccount" -Value $account
$Mail.GetInspector.Activate()
$Mail.Attachments.Add("$WorkDir\$RGMail")
$Mail.Send()}
答案2
- 您没有显示任何用于获取第一个文件的代码。
Get-ChildItem
,,Select-Object
等等。 - 你没有说明如何确定哪个文件是第一个。意思是通过名称、dateTimeStamps 等...
您所追求的并不是什么新东西,网络上和 Youtube 上都有大量示例代码。
您不一定需要 Outlook 才能做到这一点。这就是...
Send-MailMessage
...cmdlet 可用于或您可以安装和使用的众多模块之一。
Find-Module -Name '*mail*'
使用 Outlook 并没有什么问题,而且网络上也有很多例子。例如:
# Check to see we have all the arguments
if($args.Count -lt 1)
{
Write-Host "Use: SendMail.ps1 <Path>"
Write-Host
Write-Host " <Path>: Full path for the folder which contains the files"
Write-Host
exit
}
$FullPath=$args[0]
#Get an Outlook application object
$o = New-Object -com Outlook.Application
$mail = $o.CreateItem(0)
#2 = High importance message
$mail.importance = 2
$mail.subject = "This is the subject of the mail"
$mail.body = "This is the body of the email. It has been automatically generated by a script."
#separate multiple recipients with a ";"
$mail.To = <INSERT THE RECIPIENT HERE>
#$mail.CC = <OTHER RECIPIENT 1>;<OTHER RECIPIENT 2>
# Iterate over all files and only add the ones that have an .html extension
$files = Get-ChildItem $FullPath
for ($i=0; $i -lt $files.Count; $i++) {
$outfileName = $files[$i].FullName
$outfileNameExtension = $files[$i].Extension
# if the extension is the one we want, add to attachments
if($outfileNameExtension -eq ".html")
{
$mail.Attachments.Add($outfileName);
}
}
$mail.Send()
# give time to send the email
Start-Sleep 20
# quit Outlook
$o.Quit()
#end the script
exit