-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSend-OutlookEmail.ps1
41 lines (41 loc) · 1.08 KB
/
Send-OutlookEmail.ps1
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
# Send email and add a reply-to address using outlook comobject
function Send-OutlookEmail {
[CmdletBinding()]
param(
[Parameter(
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true
)]
[string]$recepient,
[string]$Subject,
[string]$Body,
[string]$To
)
begin {
if (-not $OutlookApplication) {
$OutlookApplication = New-Object -ComObject Outlook.Application
}
}
process {
try {
$Mail = $OutlookApplication.CreateItem("olMailItem")
$Mail.Subject = $Subject
$Mail.Body = $Body
$Mail.To = $To
$Mail.ReplyRecipients.Add($recepient)
$Mail.Send()
while (-not $Mail.Sent) {
Start-Sleep -Seconds 2
$Mail
}
$Mail = $null
} catch {
Write-Host "Failed to send email to '': $_"
}
}
end {
$OutlookApplication.Quit()
$OutlookApplication = $null
}
}