Control Microsoft Outlook on Windows via PowerShell COM objects.
Use Cases
- The user says "Help me check if there are any new emails today" or "Send an email to XXX"
- The user needs to search historical emails
- The user needs to view the calendar
Command Reference
Read Inbox
$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNamespace("MAPI")
$inbox = $namespace.GetDefaultFolder(6) # 6 = 收件箱
The 10 most recent emails
$inbox.Items | Sort-Object ReceivedTime -Descending | Select-Object -First 10 | ForEach-Object { Write-Output "$($.ReceivedTime) | $($.SenderName) | $($_.Subject)" }
### 发送邮件
```powershell
$outlook = New-Object -ComObject Outlook.Application
$mail = $outlook.CreateItem(0) # 0 = 邮件
$mail.To = "recipient@example.com"
$mail.Subject = "邮件主题"
$mail.Body = "邮件正文"
# $mail.Attachments.Add("C:\path\to\file.pdf")
$mail.Send()
Search Emails
$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNamespace("MAPI")
$inbox = $namespace.GetDefaultFolder(6)
# 按主题搜索
$results = $inbox.Items.Restrict("[Subject] = '关键词'")
$results | ForEach-Object { Write-Output "$($_.ReceivedTime) | $($_.Subject)" }
Read Calendar
$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNamespace("MAPI")
$calendar = $namespace.GetDefaultFolder(9) # 9 = 日历
$today = Get-Date -Format "yyyy/MM/dd"
$tomorrow = (Get-Date).AddDays(1).ToString("yyyy/MM/dd")
$events = $calendar.Items.Restrict("[Start] >= '$today' AND [Start] < '$tomorrow'")
$events | ForEach-Object { Write-Output "$($_.Start) - $($_.End) | $($_.Subject)" }
Safety Rules
- HITL confirmation is required before sending an email: show the recipient, subject, and body, and send after the user confirms
- Do not automatically open attachments
- Do not delete emails(read-only operations)