forked from rvrsh3ll/Misc-Powershell-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Send-EWSEmail.ps1
65 lines (56 loc) · 2.06 KB
/
Send-EWSEmail.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<#
Send Exchange Web Services Email
Author: Steve Borosh (@rvrsh3ll)
License: BSD 3-Clause
Required Dependencies: Exchange Web Services (EWS) Managed API dll from https://www.microsoft.com/en-us/download/details.aspx?id=42951
Optional Dependencies: None
#>
function Send-EWSEmail {
<#
.DESCRIPTION
Send an email using EWS
.EXAMPLE
Import-Module -Name 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'
Import-Module .\Send-EWSEmail.ps1
Send-EWSEmail -ServiceURL "https://outlook.office365.com/EWS/Exchange.asmx" -Recipient "[email protected]" -Subject "Important Message!" -EmailBody "All, <br> Check out the attachment." -Attachment .\WordDocument.rtf
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True)]
[String]
$ServiceURL,
[Parameter(Mandatory = $True)]
[String]
$Recipient,
[Parameter(Mandatory = $True)]
[String]
$Subject,
[Parameter(Mandatory = $False)]
[String]
$EmailBody,
[Parameter(Mandatory = $False)]
[String]
$Attachment
)
BEGIN {
$EXCHService = New-Object -TypeName Microsoft.Exchange.WebServices.Data.ExchangeService
$Credential = Get-Credential
}
PROCESS {
Write-Output "Sending...."
$EXCHService.Credentials = New-Object -TypeName Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $Credential.UserName, $Credential.GetNetworkCredential().Password
$EXCHService.AutodiscoverUrl($Credential.UserName, {$true})
$EXCHService.Url = $ServiceURL
$eMail = New-Object -TypeName Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $EXCHService
$eMail.Subject = $Subject
$eMail.Body = $EmailBody
$eMail.ToRecipients.Add($Recipient) | Out-Null
if ($Attachment) {
$email.Attachments.AddFileAttachment($Attachment) | Out-Null
}
$eMail.Send()
}
END {
Write-Output "Finished"
}
}