使用 PowerShell 创建 Outlook 电子邮件草稿 [英] Create Outlook email draft using PowerShell

查看:30
本文介绍了使用 PowerShell 创建 Outlook 电子邮件草稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 PowerShell 脚本来自动化工作过程.此过程需要填写一封电子邮件并发送给其他人.电子邮件将始终大致遵循相同类型的模板,但每次可能都不会相同,因此我想在 Outlook 中创建电子邮件草稿并打开电子邮件窗口,以便在发送前填写额外的详细信息.

I'm creating a PowerShell script to automate a process at work. This process requires an email to be filled in and sent to someone else. The email will always roughly follow the same sort of template however it will probably never be the same every time so I want to create an email draft in Outlook and open the email window so the extra details can be filled in before sending.

我在网上做了一些搜索,但我能找到的只是一些可以静默发送电子邮件的代码.代码如下:

I've done a bit of searching online but all I can find is some code to send email silently. The code is as follows:

$ol = New-Object -comObject Outlook.Application  
$mail = $ol.CreateItem(0)  
$Mail.Recipients.Add("XXX@YYY.ZZZ")  
$Mail.Subject = "PS1 Script TestMail"  
$Mail.Body = "  
Test Mail  
"  
$Mail.Send() 

简而言之,有没有人知道如何创建和保存新的 Outlook 电子邮件草稿并立即打开该草稿进行编辑?

In short, does anyone have any idea how to create and save a new Outlook email draft and immediately open that draft for editing?

推荐答案

$olFolderDrafts = 16
$ol = New-Object -comObject Outlook.Application 
$ns = $ol.GetNameSpace("MAPI")

# call the save method yo dave the email in the drafts folder
$mail = $ol.CreateItem(0)
$null = $Mail.Recipients.Add("XXX@YYY.ZZZ")  
$Mail.Subject = "PS1 Script TestMail"  
$Mail.Body = "  Test Mail  "
$Mail.save()

# get it back from drafts and update the body
$drafts = $ns.GetDefaultFolder($olFolderDrafts)
$draft = $drafts.Items | where {$_.subject -eq 'PS1 Script TestMail'}
$draft.body += "`n foo bar"
$draft.save()

# send the message
#$draft.Send()

这篇关于使用 PowerShell 创建 Outlook 电子邮件草稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆