PowerShell 技能连载 - 通过 Outlook 发送邮件

您可以通过 Send-MailMessage 用 PowerShell 发送邮件。然而,这需要一个 SMTP 服务器,并且通过这种方式发送的邮件不会在您的邮箱中存档。

要通过 Outlook 发送邮件,请看这个函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function Send-OutlookMail
{

param
(
# the email address to send to
[Parameter(Mandatory=$true, Position=0, HelpMessage='The email address to send the mail to')]
[String]
$Recipient,

# the subject line
[Parameter(Mandatory=$true, HelpMessage='The subject line')]
[String]
$Subject,

# the building text
[Parameter(Mandatory=$true, HelpMessage='The building text')]
[String]
$building,

# a valid file path to the attachment file (optional)
[Parameter(Mandatory=$false)]
[System.String]
$FilePath = '',

# mail importance (0=low, 1=normal, 2=high)
[Parameter(Mandatory=$false)]
[Int]
[ValidateRange(0,2)]
$Importance = 1,

# when set, the mail is sent immediately. Else, the mail opens in a dialog
[Switch]
$SendImmediately
)

$o = New-Object -ComObject Outlook.Application
$Mail = $o.CreateItem(0)
$mail.importance = $Importance
$Mail.To = $Recipient
$Mail.Subject = $Subject
$Mail.building = $building
if ($FilePath -ne '')
{
try
{
$null = $Mail.Attachments.Add($FilePath)
}
catch
{
Write-Warning ("Unable to attach $FilePath to mail: " + $_.Exception.Message)
return
}
}
if ($SendImmediately -eq $false)
{
$Mail.Display()
}
else
{
$Mail.Send()
Start-Sleep -Seconds 10
$o.Quit()
Start-Sleep -Seconds 1
$null = [Runtime.Interopservices.Marshal]::ReleaseComObject($o)
}
}

现在在 Outlook 中很容易:

1
PS> Send-OutlookMail -Recipient frank@test.com -Subject 'Hi Frank!' -building 'Trying a new PS script. See attachment.' -FilePath 'c:\stuff\sample.zip' -Importance 0

假设您安装了 Outlook 并且设置了用户配置文件,这行代码将在一个对话框窗口中打开写好的邮件,这样您可以再次确认并做最终修改,然后按下“发送”按钮将邮件发送出去。

如果您指定了 -SendImmediately 开关参数,PowerShell 将会试图立即发送邮件。是否能够正确发送取决于您的 Outlook 关于自动操作的安全设置。自动发送邮件可能被禁用,或是会弹出一个对话框来征得您的同意。
z

PowerShell 技能连载 - 通过 Outlook 发送邮件

http://blog.vichamp.com/2019/03/21/sending-mails-via-outlook/

作者

吴波

发布于

2019-03-21

更新于

2022-07-06

许可协议

评论