baphomet
Goto Top

PS-MAIL MIT GUI

Hallo Community,

ich bin relativ neu im Thema Powershell. Aktuell versuche ich mich an einer PS-GUI mit der ich Mails aus einen Web-Account versenden kann.

Ich stoße bisher auf folgende Herausforderungen:

1: $SMTPMessage wirft einen Fehler den ich nicht gelöst bekomme
2: $SMTPClient.Send($SMTPMessage) wirft einen Fehler den ich nicht gelöst bekomme.
3: Ich möchte auch ein Feld erzeugen, in dem ich eine Pfadangabe zu einer Datei bestimmen kann (Attachement).

- Nice to have, wäre eine ComBox innerhalb der Gui, die aufzeigt, dass der Serverzugriff i.O. ist und die Mail gesendet wurde.

Ich würde mich sehr freuen, wenn mir jemand helfen könnte... / Vielen Dank im Voraus...

Anbei der code:

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#-------------------------------------------------------------------------------------------------------#
# PRE-SETTINGS                                                                                          #
#-------------------------------------------------------------------------------------------------------#

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()


#-------------------------------------------------------------------------------------------------------#
# MAINFORM                                                                                              #
#-------------------------------------------------------------------------------------------------------#

$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='4B75686E-POWERSHELL-MAILER **FUNKY TOOLZ 4 YOUR DAILY BUSINESS**'  
$main_form.Width = 666
$main_form.Height = 555
$main_form.AutoSize = $true
$main_form.StartPosition = 'CenterScreen'  
$main_form.Topmost = $true
$main_form.BackColor = 'CornflowerBlue'  
$main_form.ControlBox=$false
$main_form.MinimizeBox=$false
$main_form.MaximizeBox = $false
$main_form.ShowInTaskbar=$false

#-------------------------------------------------------------------------------------------------------#
# FUNCTIONS                                                                                             #
#-------------------------------------------------------------------------------------------------------#


$TextBox1 = $Username.Text
$TextBox2 = $EmailPassword.Text
$TextBox3 = $EmailFrom.Text
$TextBox4 = $EmailTo.Text
$TextBox5 = $Body.Text
$Attachement????
$DropDownBox9 = $SMTPServer
$SMTPMessage = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
#$Attachment = New-Object Net.Mail.Attachment($Attachment)
#$SMTPMessage.Attachments.Add($Attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword)
$SMTPClient.Send($SMTPMessage)

#-----------------STANDALONE FUNCTIONAL---------------
#$Username = "abcde"  
#$EmailPassword = "xxxxxxx"  
#$EmailFrom = "xxxh@web.de"  
#$EmailTo = "xxx@web.de"  
#$Subject = "$env:UserName"  
#$Body = "lorem"  
#$Attachment = "c:\Users\Public\zugangsdaten.csv"  
#$SMTPServer = "smtp.web.de"  
#$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
#$Attachment = New-Object System.Net.Mail.Attachment($Attachment)
#$SMTPMessage.Attachments.Add($Attachment)
#$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
#$SMTPClient.EnableSsl = $true
#$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword)
#$SMTPClient.Send($SMTPMessage)
#---------------------------------------------------



#-------------------------------------------------------------------------------------------------------#
# LABELS & BOXES                                                                                        #
#-------------------------------------------------------------------------------------------------------#

# USERNAME
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Text = "WEB ACCOUNT USER:"  
$Label1.Location = New-Object System.Drawing.Point(10,10)
$Label1.AutoSize = $true
$main_form.Controls.Add($Label1)

$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Location = New-Object System.Drawing.Size(250,10)
$TextBox1.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox1)

# ACCOUNT PW
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "WEB ACCOUNT PASSWORD:"  
$Label2.Location = New-Object System.Drawing.Point(10,35)
$Label2.AutoSize = $true
$main_form.Controls.Add($Label2)

$TextBox2 = New-Object System.Windows.Forms.TextBox
$TextBox2.PasswordChar = "*"  
$TextBox2.Location = New-Object System.Drawing.Size(250,35)
$TextBox2.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox2)

# EMAIL-FROM
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Text = "WEB ACCOUNT E-MAIL ADDRESS:"  
$Label3.Location = New-Object System.Drawing.Point(10,60)
$Label3.AutoSize = $true
$main_form.Controls.Add($Label3)

$TextBox3 = New-Object System.Windows.Forms.TextBox
$TextBox3.Location = New-Object System.Drawing.Size(250,60)
$TextBox3.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox3)

# EMAIL-RECEIVER
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Text = "RECEIVER E-MAIL ADDRESS:"  
$Label4.Location = New-Object System.Drawing.Point(10,85)
$Label4.AutoSize = $true
$main_form.Controls.Add($Label4)

$TextBox4 = New-Object System.Windows.Forms.TextBox
$TextBox4.Location = New-Object System.Drawing.Size(250,85)
$TextBox4.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox4)

# EMAIL-SUBJECT
$Label5 = New-Object System.Windows.Forms.Label
$Label5.Text = "ENTER SUBJECT-LINE:"  
$Label5.Location = New-Object System.Drawing.Point(10,110)
$Label5.AutoSize = $true
$main_form.Controls.Add($Label5)

$TextBox5 = New-Object System.Windows.Forms.TextBox
$TextBox5.Location = New-Object System.Drawing.Size(250,110)
$TextBox5.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox5)

# EMAIL-BODY
$Label6 = New-Object System.Windows.Forms.Label
$Label6.Text = "E-MAIL BODY:"  
$Label6.Location = New-Object System.Drawing.Point(10,135)
$Label6.AutoSize = $true
$main_form.Controls.Add($Label6)

$textBox6 = New-Object System.Windows.Forms.TextBox
$textBox6.Location = New-Object System.Drawing.Point(250,135)
$textBox6.Size = New-Object System.Drawing.Size(350,100)
$textBox6.Multiline = $true
$main_form.Controls.Add($textBox6)

# EMAIL-ATTACHEMENT
$Label7 = New-Object System.Windows.Forms.Label
$Label7.Text = "ADD ATTACHEMENT:"  
$Label7.Location = New-Object System.Drawing.Point(10,260)
$Label7.AutoSize = $true
$main_form.Controls.Add($Label7)

# SMTP-LIST
$Label8 = New-Object System.Windows.Forms.Label
$Label8.Text = "PLEASE CLICK YOUR E-MAIL PROVIDER:"  
$Label8.Location = New-Object System.Drawing.Point(10,285)
$Label8.AutoSize = $true
$main_form.Controls.Add($Label8)

$DropDownBox8 = New-Object System.Windows.Forms.ComboBox
$DropDownBox8.Location = New-Object System.Drawing.Size(250,285)
$DropDownBox8.Size = New-Object System.Drawing.Size(260,20)
$DropDownBox8.DropDownHeight = 100
$main_form.Controls.Add($DropDownBox8)

[void] $DropDownBox8.Items.Add('smtp.web.de')    
[void] $DropDownBox8.Items.Add('smtp.gmail.com')    
[void] $DropDownBox8.Items.Add('smtp.mail.yahoo.com')    
[void] $DropDownBox8.Items.Add('mail.gmx.com')    

$DropDownBox.Add_SelectedIndexChanged
[string]$SMTPServer=$DropDownBox.SelectedItem


#-------------------------------------------------------------------------------------------------------#
# BUTTONS                                                                                               #
#-------------------------------------------------------------------------------------------------------#

# OK
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(430,400)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'  
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$OKButton.Add_Click({$Username=$TextBox1.Text;$EmailPasssword=$TextBox2.Text;$EmailFrom=$TextBox3.Text;$EmailTo=$TextBox4.Text;$Subject=$TextBox5.Text;$Body=$TextBox6.Text;$SMTPServer=$DropDownBox8.SelectedItem;$main_form.Close()})
$main_form.AcceptButton = $OKButton
$main_form.Controls.Add($OKButton)

# CANCEL
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(520,400)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'  
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$main_form.CancelButton = $cancelButton
$main_form.Controls.Add($cancelButton)



#-------------------------------------------------------------------------------------------------------#
# CLOSE                                                                                                 #
#-------------------------------------------------------------------------------------------------------#

# MAKE THE DIALOG BOX VISIBLE
$main_form.ShowDialog()
1

Content-ID: 71746208971

Url: https://administrator.de/forum/ps-mail-mit-gui-71746208971.html

Ausgedruckt am: 09.04.2025 um 22:04 Uhr

em-pie
em-pie 02.01.2024 um 20:19:44 Uhr
Goto Top
Moin,

Als erstes mal Zeile 38 bis 62 die #en zu Beginn entfernen. Denn dadurch, dass du die Zeilen auskommentiert hast, die die Variablen leer…
Baphomet
Baphomet 02.01.2024 um 20:27:46 Uhr
Goto Top
Hallo em-pie,

danke für diesen Hinweis👍
Jedoch sind die # in Zeile: 40+41 weil ich keine Lösung habe, das Attachement über einen FileSystem Dialog einzubinden. Meine Versuche zu FS Dialog scheitern leider alle..
Zeilen 48-62 sind auskommentiert, da diese nur die Lösung abbilden die funktioniert (ohne Gui) oder in anderen worten meine Gedankenstütze.
godlie
godlie 03.01.2024 aktualisiert um 07:59:39 Uhr
Goto Top
Hallo,


Was deinen acho so geliebten Filedialog angeht:

https://letmegooglethat.com/?q=FileChoosedialog+powershell

grüße
10138557388
10138557388 03.01.2024 aktualisiert um 09:04:33 Uhr
Goto Top
Tja wenn man die Variablen für die Mail füllt obwohl die Variablen der GUI Elemente noch gar nicht existieren ... 😉. Genau das sagen dir nämlich die Fehlermeldungen! Da stimmt die Reihenfolge nicht, das gehört in ein Event das du ausführst um die Mail zu senden nicht vor dem Erstellen der GUI 🙃. Einfach nochmal logisch nachdenken ....

Zum Thema FileDialog

1
2
3
4
$dlg = New-Object System.Windows.Forms.OpenFileDialog
if($dlg.ShowDialog() -eq 'OK'){    
    $dlg.FileNames
}

Btw. Der SMTPClient ist hoffnungslos veraltet und sollte man nicht mehr nutzen.
Für sowas nutzt man heutzutage MailKit in Powershell
https://www.powershellgallery.com/packages/Send-MailKitMessage/3.2.0

PJ.
em-pie
em-pie 03.01.2024 aktualisiert um 09:45:29 Uhr
Goto Top
Zitat von @Baphomet:
Jedoch sind die # in Zeile: 40+41 weil ich keine Lösung habe, das Attachement über einen FileSystem Dialog einzubinden. Meine Versuche zu FS Dialog scheitern leider alle..
Das wäre das "letzte" Problem, welches ich angehen würde. Erstmal die eine einfache Mail sauber raussenden können

Zeilen 48-62 sind auskommentiert, da diese nur die Lösung abbilden die funktioniert (ohne Gui) oder in anderen worten meine Gedankenstütze.
Du nutzt aber in Zeile 44 + 45 Variablen, die erst in Zeile 48-62 gefüllt werden müssten, aber auskommentiert sind. Das kann ja nicht funktionieren.
"Hey, lieber Postbote, verschicke den Brief bitte an die Infos auf dem Couvert".
Den Adressaufkleber hast du aber in deinem Notizzettel in der Schublade liegen.

Und Zeilen 32-36 sind auch Mist. Du hast ja gar keine Objekte $Username, $EmailPassword, $EmailFrom, $EmailTo erzeugt/ verfügbar.

Hau mal den Part "SendMail" in eine eigene Funktion (das macht es übersichtlicher und wartungsfreundlicher) und übergib entsprechende Parameter:
Als Beispiel, etwas rudimentär und erstmal ohne das Mail-Kit - das müsste dann entsprechend "umgebaut" werden:
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
function Send-Mail(){
    Param
    (
        [Parameter(Mandatory=$true)]
        [String] $Server,
        [Parameter(Mandatory=$true)]
        [string] $Port,
        [Parameter(Mandatory=$true)]
        [string] $Username,
        [Parameter(Mandatory=$true)]
        [string] $Password,
        [Parameter(Mandatory=$true)]
        [string] $Sender,
        [Parameter(Mandatory=$true)]
        [string] $Recipients,
        [Parameter(Mandatory=$true)]
        [string] $Subject,
        [Parameter(Mandatory=$true)]
        [string] $Body,
        [Parameter(Mandatory=$false)]
        [string] $Attachments
    )

    ## Variablen definieren
    $SmtpServer = $Server
    $SmtpPort = $Port
    $SmtpUsername = $UserName  
    $SmtpPassword = $Password  #Für den initialen Test so OK, sollte man aber besser absichern!
    
    $EmailFrom = $Sender 
    $EmailTo = $Recipients  
    $EmailSubject = $Subject #"$env:UserName"    
    $EmailBody = $Body  
    $EmailAttachment = $Attachments #"c:\Users\Public\zugangsdaten.csv"    
    
    # Objekte erzeugen
    $SmtpMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $EmailSubject, $EmailBody)
    $SmtpAttachment = New-Object System.Net.Mail.Attachment($Attachment)
    $SmtpMessage.Attachments.Add($SmtpAttachment)

    $SmtpClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SmtpPort)
    $SmtpClient.EnableSsl = $true
    $SmtpClient.Credentials = New-Object System.Net.NetworkCredential($SmtpUsername, $SmtpPassword)
    $SmtpClient.Send($SmtpMessage)
    
}

## Aufruf der Funktion
## die einzelnen Parameter könnte man zuvor auch in ein Objekt @Parameter schreiben und nur das Objekt an die Funktion übergeben
Send-Mail `
    -Server 'smtp.web.de' `  
    -Port 587`
    -Username 'liesa.mueller@web.de'`  
    -Password '#TopSecret!!!!!!11111'`  
    -Sender 'max.mueller@web.de' `  
    -Recipients 'lisa.lustig@t-online.de' `  
    -Subject 'This is a Test' `  
    -Body 'Hallo Lisa, heute scheint die Sonne' `  
    -Attachments 'c:\Users\Public\zugangsdaten.csv'  

man könnte auch, um nicht die ganzen Zugangsdaten an die Funktion übergeben zu müssen, in der Funktion selbst die Zugangsdaten hinterlegen und an die Funktion nur einen Provider übergeben

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
function Send-Mail(){
    Param
    (
        [Parameter(Mandatory=$true)]
        [String] $Provider,
        [Parameter(Mandatory=$true)]
        [string] $Sender,
        [Parameter(Mandatory=$true)]
        [string] $Recipients,
        [Parameter(Mandatory=$true)]
        [string] $Subject,
        [Parameter(Mandatory=$true)]
        [string] $Body,
        [Parameter(Mandatory=$False)]
        [string] $Attachments
    )

## Variablen definieren

    Switch ($Provider) {
        "web.de" {  
            $SmtpServer = 'smtp.web.de'  
            $SmtpPort = 587
            $SmtpUsername = 'lisa.mueller@web.de'  
            $SmtpPassword = '#Geheim.123456789!!!'  #Für den initialen Test so OK, sollte man aber besser absichern!  
            $EmailFrom = 'lisa.mueller@web.de'  
            }
        "t-onlline.de" {  
            $SmtpServer = 'securesmtp.t-online.de'  
            $SmtpPort = 587
            $SmtpUsername = 'hans.hiebbelig@t-online.de'  
            $SmtpPassword = '#AuchGeheim.!!!'  #Für den initialen Test so OK, sollte man aber besser absichern!  
            $EmailFrom = 'hans.hiebbelig@t-online.de'  
            }
        Default {
            $SmtpServer = 'smtp.google.com'  
            $SmtpPort = 587
            $SmtpUsername = 'ich@gmail.com'  
            $SmtpPassword = '#KenntGoogle'  #Für den initialen Test so OK, sollte man aber besser absichern!  
            $EmailFrom = 'ich@gmail.com'  
            }
    }

    $EmailTo = $Recipients  
    $EmailSubject = $Subject #"$env:UserName"    
    $EmailBody = $Body  
    $EmailAttachment = $Attachments #"c:\Users\Public\zugangsdaten.csv"    

    # Objekte erzeugen
    $SmtpMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $EmailSubject, $EmailBody)
    $SmtpAttachment = New-Object System.Net.Mail.Attachment($Attachment)
    $SmtpMessage.Attachments.Add($SmtpAttachment)

    $SmtpClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SmtpPort)
    $SmtpClient.EnableSsl = $true
    $SmtpClient.Credentials = New-Object System.Net.NetworkCredential($SmtpUsername, $SmtpPassword)
    $SmtpClient.Send($SmtpMessage)

}

## Aufruf der Funktion
## die einzelnen Parameter könnte man zuvor auch in ein Objekt @Parameter schreiben und nur das Objekt an die Funktion übergeben
Send-Mail `
    -Provider 'web.de'`  
    -Recipients 'lisa.lustig@t-online.de' `  
    -Subject 'This is a Test' `  
    -Body 'Hallo Lisa, heute scheint die Sonne' `  
    -Attachments 'c:\Users\Public\zugangsdaten.csv'  

Beides muss da sicherlich mal Ausgebaut werden, auch im Hinblick auf Fehlerabfangen etc.

Und die Funktion kannst du dann auch in deinem Code beim ".Click"-Event ausführen:
1
2
3
4
5
6
$OKButton.Add_Click({Send-Mail `
    -Provider 'web.de'`  
    -Recipients 'lisa.lustig@t-online.de' `  
    -Subject 'This is a Test' `  
    -Body 'Hallo Lisa, heute scheint die Sonne' `  
    -Attachments 'c:\Users\Public\zugangsdaten.csv' })  
Baphomet
Baphomet 03.01.2024 um 10:18:33 Uhr
Goto Top
Vielen Dank für Eure Anregungen 👍

@godlie, diesen Artikel habe ich auch bereits gesehen
@10138557388, danke

-> Aufgrund der Verwirrung habe ich nun die Gedankenstütze entnommen.
-> Auskommentierte Felder sind geöffnet.
-> Ich habe einen Button für die Dateisuche angelegt.

Obschon ich schon viele von diesen Lösungen versucht habe, ich bekomme es dennoch nicht hin, den Filebrowser auf den Button zu legen($FileButton7) so dass sich der Dialog öffnet. Ich sehe wenn ja auch nicht ob die Datei nun angezogen wurde?

Ich glaube ich habe mich einfach sehr verstrickt und sehe den Wald vor Bäumen nicht mehr.
Von daher nochmals die freundliche Bitte um Unterstützung.

Code Neu:
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#-------------------------------------------------------------------------------------------------------#
# PRE-SETTINGS                                                                                          #
#-------------------------------------------------------------------------------------------------------#

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()


#-------------------------------------------------------------------------------------------------------#
# MAINFORM                                                                                              #
#-------------------------------------------------------------------------------------------------------#

$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='4B75686E-POWERSHELL-MAILER **FUNKY TOOLZ 4 YOUR DAILY BUSINESS**'  
$main_form.Width = 666
$main_form.Height = 555
$main_form.AutoSize = $true
$main_form.StartPosition = 'CenterScreen'  
$main_form.Topmost = $true
$main_form.BackColor = 'CornflowerBlue'  
$main_form.ControlBox=$false
$main_form.MinimizeBox=$false
$main_form.MaximizeBox = $false
$main_form.ShowInTaskbar=$false

#-------------------------------------------------------------------------------------------------------#
# FUNCTIONS                                                                                             #
#-------------------------------------------------------------------------------------------------------#


$TextBox1 = $Username.Text
$TextBox2 = $EmailPassword.Text
$TextBox3 = $EmailFrom.Text
$TextBox4 = $EmailTo.Text
$TextBox5 = $Body.Text
$Attachement????
$DropDownBox9 = $SMTPServer
$SMTPMessage = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
$Attachment = New-Object Net.Mail.Attachment($Attachment)
$SMTPMessage.Attachments.Add($Attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword)
$SMTPClient.Send($SMTPMessage)

#-------------------------------------------------------------------------------------------------------#
# LABELS & BOXES                                                                                        #
#-------------------------------------------------------------------------------------------------------#

# USERNAME
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Text = "WEB ACCOUNT USER:"  
$Label1.Location = New-Object System.Drawing.Point(10,10)
$Label1.AutoSize = $true
$main_form.Controls.Add($Label1)

$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Location = New-Object System.Drawing.Size(250,10)
$TextBox1.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox1)

# ACCOUNT PW
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "WEB ACCOUNT PASSWORD:"  
$Label2.Location = New-Object System.Drawing.Point(10,35)
$Label2.AutoSize = $true
$main_form.Controls.Add($Label2)

$TextBox2 = New-Object System.Windows.Forms.TextBox
$TextBox2.PasswordChar = "*"  
$TextBox2.Location = New-Object System.Drawing.Size(250,35)
$TextBox2.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox2)

# EMAIL-FROM
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Text = "WEB ACCOUNT E-MAIL ADDRESS:"  
$Label3.Location = New-Object System.Drawing.Point(10,60)
$Label3.AutoSize = $true
$main_form.Controls.Add($Label3)

$TextBox3 = New-Object System.Windows.Forms.TextBox
$TextBox3.Location = New-Object System.Drawing.Size(250,60)
$TextBox3.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox3)

# EMAIL-RECEIVER
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Text = "RECEIVER E-MAIL ADDRESS:"  
$Label4.Location = New-Object System.Drawing.Point(10,85)
$Label4.AutoSize = $true
$main_form.Controls.Add($Label4)

$TextBox4 = New-Object System.Windows.Forms.TextBox
$TextBox4.Location = New-Object System.Drawing.Size(250,85)
$TextBox4.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox4)

# EMAIL-SUBJECT
$Label5 = New-Object System.Windows.Forms.Label
$Label5.Text = "ENTER SUBJECT-LINE:"  
$Label5.Location = New-Object System.Drawing.Point(10,110)
$Label5.AutoSize = $true
$main_form.Controls.Add($Label5)

$TextBox5 = New-Object System.Windows.Forms.TextBox
$TextBox5.Location = New-Object System.Drawing.Size(250,110)
$TextBox5.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox5)

# EMAIL-BODY
$Label6 = New-Object System.Windows.Forms.Label
$Label6.Text = "E-MAIL BODY:"  
$Label6.Location = New-Object System.Drawing.Point(10,135)
$Label6.AutoSize = $true
$main_form.Controls.Add($Label6)

$textBox6 = New-Object System.Windows.Forms.TextBox
$textBox6.Location = New-Object System.Drawing.Point(250,135)
$textBox6.Size = New-Object System.Drawing.Size(350,100)
$textBox6.Multiline = $true
$main_form.Controls.Add($textBox6)

# EMAIL-ATTACHEMENT
$Label7 = New-Object System.Windows.Forms.Label
$Label7.Text = "ADD ATTACHEMENT:"  
$Label7.Location = New-Object System.Drawing.Point(10,260)
$Label7.AutoSize = $true
$main_form.Controls.Add($Label7)


# SMTP-LIST
$Label8 = New-Object System.Windows.Forms.Label
$Label8.Text = "PLEASE CLICK YOUR E-MAIL PROVIDER:"  
$Label8.Location = New-Object System.Drawing.Point(10,285)
$Label8.AutoSize = $true
$main_form.Controls.Add($Label8)

$DropDownBox8 = New-Object System.Windows.Forms.ComboBox
$DropDownBox8.Location = New-Object System.Drawing.Size(250,285)
$DropDownBox8.Size = New-Object System.Drawing.Size(260,20)
$DropDownBox8.DropDownHeight = 100
$main_form.Controls.Add($DropDownBox8)

[void] $DropDownBox8.Items.Add('smtp.web.de')    
[void] $DropDownBox8.Items.Add('smtp.gmail.com')    
[void] $DropDownBox8.Items.Add('smtp.mail.yahoo.com')    
[void] $DropDownBox8.Items.Add('mail.gmx.com')    

$DropDownBox.Add_SelectedIndexChanged
[string]$SMTPServer=$DropDownBox.SelectedItem


#-------------------------------------------------------------------------------------------------------#
# BUTTONS                                                                                               #
#-------------------------------------------------------------------------------------------------------#

# ATTACHEMENT-BUTTON
$FileButton7 = New-Object System.Windows.Forms.Button
$FileButton7.Location = New-Object System.Drawing.Point(250,260)
$FileButton7.Size = New-Object System.Drawing.Size(115,23)
$FileButton7.Text = 'OPEN FILE-TREE'  
$FileButton7.DialogResult = [System.Windows.Forms.DialogResult]::FILE
$main_form.AcceptButton = $FileButton7
$main_form.Controls.Add($FileButton7)

# OK
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(430,400)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'  
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$OKButton.Add_Click({$Username=$TextBox1.Text;$EmailPasssword=$TextBox2.Text;$EmailFrom=$TextBox3.Text;$EmailTo=$TextBox4.Text;$Subject=$TextBox5.Text;$Body=$TextBox6.Text;$SMTPServer=$DropDownBox8.SelectedItem;$main_form.Close()})
$main_form.AcceptButton = $OKButton
$main_form.Controls.Add($OKButton)

# CANCEL
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(520,400)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'  
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$main_form.CancelButton = $cancelButton
$main_form.Controls.Add($cancelButton)



#-------------------------------------------------------------------------------------------------------#
# CLOSE                                                                                                 #
#-------------------------------------------------------------------------------------------------------#

# MAKE THE DIALOG BOX VISIBLE
$main_form.ShowDialog()
Baphomet
Baphomet 03.01.2024 um 10:41:56 Uhr
Goto Top
Hallo @em-pie,

danke für die snippets.

-> Der User sollte nicht hinterlegt sein, da dass Multiuser sein soll.
-> Ich habe Deine Aussage verstanden dass die Funktionen fehlen. Ich versuche es in einer 2 Variante mal umzubauen.
-> Was mich wundert, selbst wenn ich alle ATTACHEMENT auskommentiere, bekommen ich den Fehler in Z39 -> Das möchte ich ja in der Gui ausfüllen & Z45 muss ja durch den OK Button erfolgen??
Baphomet
Baphomet 03.01.2024 um 12:06:01 Uhr
Goto Top
-> FileDialog eingefügt
-> Auswahl eingefügt

Jetzt hängt es nur noch an den Verbindungen der bisherigen Lösung.

Code neu (3)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#-------------------------------------------------------------------------------------------------------#
# PRE-SETTINGS                                                                                          #
#-------------------------------------------------------------------------------------------------------#

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()


#-------------------------------------------------------------------------------------------------------#
# MAINFORM                                                                                              #
#-------------------------------------------------------------------------------------------------------#

$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='4B75686E-POWERSHELL-MAILER **FUNKY TOOLZ 4 YOUR DAILY BUSINESS**'  
$main_form.Width = 666
$main_form.Height = 555
$main_form.AutoSize = $true
$main_form.StartPosition = 'CenterScreen'  
$main_form.Topmost = $true
$main_form.BackColor = 'CornflowerBlue'  
$main_form.ControlBox=$false
$main_form.MinimizeBox=$false
$main_form.MaximizeBox = $false
$main_form.ShowInTaskbar=$false

#-------------------------------------------------------------------------------------------------------#
# FUNCTIONS                                                                                             #
#-------------------------------------------------------------------------------------------------------#


$TextBox1 = $Username.Text
$TextBox2 = $EmailPassword.Text
$TextBox3 = $EmailFrom.Text
$TextBox4 = $EmailTo.Text
$TextBox5 = $Body.Text
#$Attachement????
$DropDownBox9 = $SMTPServer
$SMTPMessage = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
#$Attachment = New-Object Net.Mail.Attachment($Attachment)
#$SMTPMessage.Attachments.Add($Attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword)
$SMTPClient.Send($SMTPMessage)

# FILEDIALOG

function Select-File ($InitialDirectory) {
    Add-Type -AssemblyName System.Windows.Forms
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.Title = "Please Select File"  
    $OpenFileDialog.InitialDirectory = $InitialDirectory
    $OpenFileDialog.filter = "All files (*.*)| *.*"  
    If ($OpenFileDialog.ShowDialog() -eq "Cancel") {  
        [System.Windows.Forms.MessageBox]::Show("No File Selected. Please select a file !", "Error", 0,   
        [System.Windows.Forms.MessageBoxIcon]::Exclamation)
        $result = $null
    } 
    else { 
        $result = $OpenFileDialog.FileName
    }
    $OpenFileDialog.Dispose()

    return $result
}

function Get-IniContent ($FilePath) {
    $ini = @{}
    switch -regex -file $FilePath
    {
        "^\[(.+)\]" # Section  
        {
            $section = $matches[1]
            $ini[$section] = @{}
            $CommentCount = 0
        }
        "^(;.*)$" # Comment  
        {
            $value = $matches[1]
            $CommentCount = $CommentCount + 1
            $name = "Comment" + $CommentCount  
            $ini[$section][$name] = $value
        } 
        "(.+?)\s*=(.*)" # Key  
        {
            $name,$value = $matches[1..2]
            $ini[$section][$name] = $value
        }
    }
    return $ini
}

# NO CHOICE SET NULL
$selectedFile = if ($Sel.Text -ne "Selected") { $Sel.Text } else { $null }  

# FILECHECK
if ($selectedFile -and (Test-Path -Path $selectedFile -PathType Leaf)) {
    Write-Host "Reading INI file '$($selectedFile)'"  
    $iniContent = Get-IniContent $selectedFile
    $Region = $iniContent["Location"]  
    $region
}
else {
    Write-Warning "The dialog was cancelled or the selected file cannot be found."  
}

#-------------------------------------------------------------------------------------------------------#
# LABELS & BOXES                                                                                        #
#-------------------------------------------------------------------------------------------------------#

# USERNAME
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Text = "WEB ACCOUNT USER:"  
$Label1.Location = New-Object System.Drawing.Point(10,10)
$Label1.AutoSize = $true
$main_form.Controls.Add($Label1)

$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Location = New-Object System.Drawing.Size(250,10)
$TextBox1.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox1)

# ACCOUNT PW
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "WEB ACCOUNT PASSWORD:"  
$Label2.Location = New-Object System.Drawing.Point(10,35)
$Label2.AutoSize = $true
$main_form.Controls.Add($Label2)

$TextBox2 = New-Object System.Windows.Forms.TextBox
$TextBox2.PasswordChar = "*"  
$TextBox2.Location = New-Object System.Drawing.Size(250,35)
$TextBox2.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox2)

# EMAIL-FROM
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Text = "WEB ACCOUNT E-MAIL ADDRESS:"  
$Label3.Location = New-Object System.Drawing.Point(10,60)
$Label3.AutoSize = $true
$main_form.Controls.Add($Label3)

$TextBox3 = New-Object System.Windows.Forms.TextBox
$TextBox3.Location = New-Object System.Drawing.Size(250,60)
$TextBox3.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox3)

# EMAIL-RECEIVER
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Text = "RECEIVER E-MAIL ADDRESS:"  
$Label4.Location = New-Object System.Drawing.Point(10,85)
$Label4.AutoSize = $true
$main_form.Controls.Add($Label4)

$TextBox4 = New-Object System.Windows.Forms.TextBox
$TextBox4.Location = New-Object System.Drawing.Size(250,85)
$TextBox4.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox4)

# EMAIL-SUBJECT
$Label5 = New-Object System.Windows.Forms.Label
$Label5.Text = "ENTER SUBJECT-LINE:"  
$Label5.Location = New-Object System.Drawing.Point(10,110)
$Label5.AutoSize = $true
$main_form.Controls.Add($Label5)

$TextBox5 = New-Object System.Windows.Forms.TextBox
$TextBox5.Location = New-Object System.Drawing.Size(250,110)
$TextBox5.Size = New-Object System.Drawing.Size(350,20)
$main_form.Controls.Add($TextBox5)

# EMAIL-BODY
$Label6 = New-Object System.Windows.Forms.Label
$Label6.Text = "E-MAIL BODY:"  
$Label6.Location = New-Object System.Drawing.Point(10,135)
$Label6.AutoSize = $true
$main_form.Controls.Add($Label6)

$textBox6 = New-Object System.Windows.Forms.TextBox
$textBox6.Location = New-Object System.Drawing.Point(250,135)
$textBox6.Size = New-Object System.Drawing.Size(350,100)
$textBox6.Multiline = $true
$main_form.Controls.Add($textBox6)

# EMAIL-ATTACHEMENT
$Label7 = New-Object System.Windows.Forms.Label
$Label7.Text = "ADD ATTACHEMENT:"  
$Label7.Location = New-Object System.Drawing.Point(10,260)
$Label7.AutoSize = $true
$main_form.Controls.Add($Label7)

$textBox7 = New-Object system.Windows.Forms.TextBox
$textBox7.location = New-Object System.Drawing.Point(380,260)
$textBox7.Size = New-Object System.Drawing.Size(220,20)
$textBox7.Text = "Selected"  
$main_form.Controls.Add($textBox7)


# SMTP-LIST
$Label8 = New-Object System.Windows.Forms.Label
$Label8.Text = "PLEASE CLICK YOUR E-MAIL PROVIDER:"  
$Label8.Location = New-Object System.Drawing.Point(10,285)
$Label8.AutoSize = $true
$main_form.Controls.Add($Label8)

$DropDownBox8 = New-Object System.Windows.Forms.ComboBox
$DropDownBox8.Location = New-Object System.Drawing.Size(250,285)
$DropDownBox8.Size = New-Object System.Drawing.Size(350,20)
$DropDownBox8.DropDownHeight = 100
$main_form.Controls.Add($DropDownBox8)

[void] $DropDownBox8.Items.Add('smtp.web.de')    
[void] $DropDownBox8.Items.Add('smtp.gmail.com')    
[void] $DropDownBox8.Items.Add('smtp.mail.yahoo.com')    
[void] $DropDownBox8.Items.Add('mail.gmx.com')    

$DropDownBox.Add_SelectedIndexChanged
[string]$SMTPServer=$DropDownBox.SelectedItem


#-------------------------------------------------------------------------------------------------------#
# BUTTONS                                                                                               #
#-------------------------------------------------------------------------------------------------------#

# ATTACHEMENT-BUTTON
$FileButton7 = New-Object System.Windows.Forms.Button
$FileButton7.Location = New-Object System.Drawing.Point(250,260)
$FileButton7.Size = New-Object System.Drawing.Size(115,20)
$FileButton7.Text = 'OPEN FILE-TREE'  
$FileButton7.Add_Click({ $textBox7.Text = Select-File })
$main_form.AcceptButton = $FileButton7
$main_form.Controls.Add($FileButton7)

# OK
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(430,400)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'  
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$OKButton.Add_Click({$Username=$TextBox1.Text;$EmailPasssword=$TextBox2.Text;$EmailFrom=$TextBox3.Text;$EmailTo=$TextBox4.Text;$Subject=$TextBox5.Text;$Body=$TextBox6.Text;$SMTPServer=$DropDownBox8.SelectedItem;$main_form.Close()})
$main_form.AcceptButton = $OKButton
$main_form.Controls.Add($OKButton)

# CANCEL
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(520,400)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'  
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$main_form.CancelButton = $cancelButton
$main_form.Controls.Add($cancelButton)



#-------------------------------------------------------------------------------------------------------#
# CLOSE                                                                                                 #
#-------------------------------------------------------------------------------------------------------#

# MAKE THE DIALOG BOX VISIBLE
$main_form.ShowDialog()
em-pie
Lösung em-pie 03.01.2024 um 12:19:18 Uhr
Goto Top
-> Was mich wundert, selbst wenn ich alle ATTACHEMENT auskommentiere, bekommen ich den Fehler in Z39 -> Das möchte ich ja in der Gui ausfüllen & Z45 muss ja durch den OK Button erfolgen??
weil du hier Variablen verwendest, die es zu dem Zeitpunkt im Code noch nicht gibt.

Schreibe das ganze SMTP-Zeugs in eine Funktion und rufe die Funktion dann in deinem OK-Button auf, bei dem due die Parameter übergibst.
Die Lösung hast du bei mir im ersten Code-Snippet und kannst den Funktionsaufruf aus dem dritten Snippet ja adaptieren.

Bei PowerShell gilt (anders als u. a. bei VBS)
erst alle Funktionen/ Variablen definieren und dann mit deren Aufrufen arbeiten.

Himmel, lies doch, was wir schreiben:
Zitat von @10138557388
Tja wenn man die Variablen für die Mail füllt obwohl die Variablen der GUI Elemente noch gar nicht existieren ... 😉. Genau das sagen dir nämlich die Fehlermeldungen! Da stimmt die Reihenfolge nicht, das gehört in ein Event das du ausführst um die Mail zu senden nicht vor dem Erstellen der GUI 🙃. Einfach nochmal logisch nachdenken ....