-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfunction-SendMail-example.ps1
More file actions
96 lines (74 loc) · 3.58 KB
/
function-SendMail-example.ps1
File metadata and controls
96 lines (74 loc) · 3.58 KB
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
90
91
92
93
94
95
96
#----------------------------------------------------------[Declarations]----------------------------------------------------------
### function SendMail
[string] $strEmailServerHostname = "smtp.wydler.eu"
[int] $intEmailServerPort = 25
[string] $strEMailAbsender = "noreply@blog.wydler.eu"
[string] $strEMailSubject = "[TEST] E-Mailversand"
$strEmailBody = @"
Sehr geehrte Damen und Herren,
hier könnte ihr Text stehen.
***Dies ist eine automatisch generierte E-Mail von einer System-E-Mail-Adresse. Bitte antworten Sie nicht auf diese E-Mail.***
"@
[string] $strEmailEmpfaenger = ""
#-----------------------------------------------------------[Functions]------------------------------------------------------------
Function SendMail {
param (
[Parameter(Mandatory=$True,Position=1)]
[string] $MailServerName,
[Parameter(Mandatory=$False,Position=2)]
[int] $MailServerPort = 25,
[Parameter(Mandatory=$True,Position=3)]
[string] $EmailAbsender,
[Parameter(Mandatory=$True,Position=4)]
[string] $EmailEmpfaenger,
[Parameter(Mandatory=$True,Position=5)]
[string] $EmailSubject,
[Parameter(Mandatory=$True,Position=6)]
[string] $EmailBody,
[Parameter(Mandatory=$False)]
[bool] $Tls = $false,
[Parameter(Mandatory=$False)]
[string] $MailServerAuthUser,
[Parameter(Mandatory=$False)]
[string] $EmailAttachment
)
# Neues Objekt
$oSmtpClient = New-Object System.Net.Mail.SmtpClient($MailServerName, $MailServerPort)
$oSmtpMessage = New-Object System.Net.Mail.MailMessage($EmailAbsender, $EmailEmpfaenger, $EmailSubject, $EmailBody)
# (De)aktivert TLS
$oSmtpClient.EnableSSL = $Tls
# Prüfe, ob ein Benutzer angegeben wurde
if($MailServerAuthUser) {
# Prüfe, ob das cmdlet PSCredential geladen ist
if ( Get-Command Get-PSCredential -ErrorAction SilentlyContinue) {
# Output
Write-Host "Das cmdlet PSCredential wurde eingebunden." -ForegroundColor Green
#Prüft ob für den angegeben Credentials vorhanden sind und fragt ggf. nach dem Passwort
$oSmtpClient.Credentials = Get-PSCredential "$MailServerAuthUser"
}
else {
return Write-host "Das cmdlet PSCredential konnte nicht gefunden werden!" -ForegroundColor Red
exit
}
}
# Attach Attachments
if ($EmailAttachment) {
$oSmtpMessageAttachment = New-Object System.Net.Mail.Attachment("$EmailAttachment")
$oSmtpMessage.Attachments.Add($oSmtpMessageAttachment)
}
# E-Mail verschicken
try {
Write-Host "`nE-Mail wird versendet..."
$oSmtpClient.Send($oSmtpMessage)
return Write-Host "E-Mail wurde verschickt." -ForegroundColor Green
}
catch [exception] {
Write-Host $("Fehler: " + $_.Exception.GetType().FullName) -ForegroundColor Red
Write-Host $("Fehler: " + $_.Exception.Message + "`n") -ForegroundColor Red
}
}
#-----------------------------------------------------------[Execution]------------------------------------------------------------
SendMail -MailServerName $strEmailServerHostname -MailServerPort $intEmailServerPort -EmailAbsender $strEMailAbsender `
-EmailEmpfaenger $strEmailEmpfaenger -EmailSubject $strEMailSubject -EmailBody "$strEmailBody"
SendMail -MailServerName $strEmailServerHostname -MailServerPort $intEmailServerPort -EmailAbsender $strEMailAbsender `
-EmailEmpfaenger $strEmailEmpfaenger -EmailSubject $strEMailSubject -EmailBody "$strEmailBody" -EmailAttachments $strPathToPdfFile