AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1668849
Accepted
ASD
ASD
Asked: 2021-08-12 08:00:27 +0800 CST2021-08-12 08:00:27 +0800 CST 2021-08-12 08:00:27 +0800 CST

Powershell Outlook 电子邮件、附件、主题

  • 772

所以我得到了一个包含不同 PDF 文件的文件夹(“RG 330526.pdf”、“RG 330527.pdf”、“RG 330528.pdf”)等。

现在,我想创建一个新的电子邮件,附加文件夹中的第一个 pdf (C:\1_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()
microsoft-outlook powershell
  • 2 2 个回答
  • 277 Views

2 个回答

  • Voted
  1. Best Answer
    ASD
    2021-08-14T10:47:58+08:002021-08-14T10:47:58+08:00

    搞定了!

    这对我有用:

    $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()}
    
    • 1
  2. postanote
    2021-08-12T08:50:08+08:002021-08-12T08:50:08+08:00
    1. 您没有显示任何用于获取第一个文件的代码。Get-ChildItem, Select-Object, 等等..
    2. 你不是说你是如何确定文件的,这是第一个。名称、日期时间戳等的含义...

    您所追求的并不是新事物,网络和 Youtube 上都有大量示例代码。

    您不一定需要 Outlook 来执行此操作。这就是...

    Send-MailMessage 
    

    ...cmdlet 可用于您可以安装和使用的众多模块之一。

    Find-Module -Name '*mail*'
    

    使用 Outlook 没有任何问题,而且网络上还有很多示例。例如:

    https://blogit.create.pt/andresilva/2017/12/07/sending-an-email-with-attachments-using-outlook-and-powershell/

    # 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
    
    • 0

相关问题

  • 将前景颜色添加到 Powershell 配置文件?

  • 防止保存 PST 文件密码?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

  • 主收件箱文件夹中的 Outlook 视图已更改,但其他文件夹未更改

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve