-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathSend-EWSEmail.ps1
89 lines (80 loc) · 3.07 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<#
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 {
$Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Provider.CreateCompiler()
$Params=New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable=$False
$Params.GenerateInMemory=$True
$Params.IncludeDebugInformation=$False
$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
$TASource=@'
namespace Local.ToolkitExtensions.Net.CertificatePolicy{
public class TrustAll : System.Net.ICertificatePolicy {
public TrustAll() {
}
public bool CheckValidationResult(System.Net.ServicePoint sp,
System.Security.Cryptography.X509Certificates.X509Certificate cert,
System.Net.WebRequest req, int problem) {
return true;
}
}
}
'@
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
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"
}
}