SMTP Tester in Powershell

The below script is a simple method to test if port 25 is open on a server. The script will attempt to connect to the server and send a quick email with a small attachment that it creates on the fly.

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

<pre class="brush: powershell; title: ; notranslate" title="">######################################################################
# - Company SMTP Tester Tool
######################################################################

$host.ui.RawUI.WindowTitle = "Company SMTP Tester Tool"

$xAppName&nbsp;&nbsp;&nbsp; = "Company_SMTP_Tester_Tool"
[BOOLEAN]$global:xExitSession=$false
function LoadMenuSystem(){
[INT]$MainMenu=0
[INT]$SubMenu=0
[BOOLEAN]$xValidSelection=$false
while ( $MainMenu -lt 1 -or $MainMenu -gt 5 ){
CLS

#############################
# - Present the Main Menu Options to the user
#############################
Write-Host "`n`tCompany SMTP Tester Tool - Version 1.0`n" -ForegroundColor Magenta
Write-Host "`t`tPlease select a location`n" -Fore Cyan
Write-Host "`t`t`t1. Test SMTP" -Fore Cyan
Write-Host "`t`t`t2. Colo SMTP" -Fore Cyan
Write-Host "`t`t`t3. London SMTP" -Fore Cyan
Write-Host "`t`t`t4. Load Balancer`n" -Fore Cyan
Write-Host "`t`t`t5. Quit and Exit`n" -Fore Yellow
# -&nbsp; Retrieve the response from the user
[int]$MainMenu = Read-Host "`t`tEnter Location Number"
if( $MainMenu -lt 1 -or $MainMenu -gt 5 ){
Write-Host "`tPlease select one of the avialble locations.`n" -Fore Red;start-Sleep -Seconds 1
}
}
Switch ($MainMenu){

#############################
# - Test SMTP
#############################
1 {
#############################
# - Set correct SMTP Server to test with
#############################

$PSEmailServer = "127.0.0.1"

#############################
# - Create temp file to send
#############################

$tempFile = [System.IO.Path]::GetTempFileName()

#############################
# - Ask for the Sender and Reciver of the email
#############################

Write-Host "`n`nWhat is the sending email address?`n" -Fore Yellow;
$sendingemail = [Console]::ReadLine();

Write-Host "`nWho will recieve this email?`n" -Fore Yellow;
$recieveemail = [Console]::ReadLine();

#############################
# - Send the email based on user input and kill temp file
#############################

Send-MailMessage -From "$sendingemail" -To "$recieveemail" -Subject "Test email from Company" -Body "This is a test email from Company IT Support, Please ignore this." -Attachments "$tempFile" -dno onSuccess, onFailure -smtpServer $PSEmailServer
Remove-Item -Force $tempFile

#############################
# - Let the user know you sent the email
#############################

Write-Host "`n`nAn email has been sent from $sendingemail to $recieveemail`n" -Fore Yellow;
Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

#############################
# - Colo SMTP
#############################
2 {
#############################
# - Set correct SMTP Server to test with
#############################

$PSEmailServer = "127.0.0.1"

#############################
# - Create temp file to send
#############################

$tempFile = [System.IO.Path]::GetTempFileName()

#############################
# - Ask for the Sender and Reciver of the email
#############################

Write-Host "`n`nWhat is the sending email address?`n" -Fore Yellow;
$sendingemail = [Console]::ReadLine();

Write-Host "`nWho will recieve this email?`n" -Fore Yellow;
$recieveemail = [Console]::ReadLine();

#############################
# - Send the email based on user input and kill temp file
#############################

Send-MailMessage -From "$sendingemail" -To "$recieveemail" -Subject "Test email from Company" -Body "This is a test email from Company IT Support, Please ignore this." -Attachments "$tempFile" -dno onSuccess, onFailure -smtpServer $PSEmailServer
Remove-Item -Force $tempFile

#############################
# - Let the user know you sent the email
#############################

Write-Host "`n`nAn email has been sent from $sendingemail to $recieveemail`n" -Fore Yellow;
Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

#############################
# - London SMTP
#############################
3 {
#############################
# - Set correct SMTP Server to test with
#############################

$PSEmailServer = "127.0.0.1"

#############################
# - Create temp file to send
#############################

$tempFile = [System.IO.Path]::GetTempFileName()

#############################
# - Ask for the Sender and Reciver of the email
#############################

Write-Host "`n`nWhat is the sending email address?`n" -Fore Yellow;
$sendingemail = [Console]::ReadLine();

Write-Host "`nWho will recieve this email?`n" -Fore Yellow;
$recieveemail = [Console]::ReadLine();

#############################
# - Send the email based on user input and kill temp file
#############################

Send-MailMessage -From "$sendingemail" -To "$recieveemail" -Subject "Test email from Company" -Body "This is a test email from Company IT Support, Please ignore this." -Attachments "$tempFile" -dno onSuccess, onFailure -smtpServer $PSEmailServer
Remove-Item -Force $tempFile

#############################
# - Let the user know you sent the email
#############################

Write-Host "`n`nAn email has been sent from $sendingemail to $recieveemail`n" -Fore Yellow;
Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

#############################
# - Load Balancer
#############################
4 {
#############################
# - Set correct SMTP Server to test with
#############################

$PSEmailServer = "127.0.01"

#############################
# - Create temp file to send
#############################

$tempFile = [System.IO.Path]::GetTempFileName()

#############################
# - Ask for the Sender and Reciver of the email
#############################

Write-Host "`n`nWhat is the sending email address?`n" -Fore Yellow;
$sendingemail = [Console]::ReadLine();

Write-Host "`nWho will recieve this email?`n" -Fore Yellow;
$recieveemail = [Console]::ReadLine();

#############################
# - Send the email based on user input and kill temp file
#############################

Send-MailMessage -From "$sendingemail" -To "$recieveemail" -Subject "Test email from Company" -Body "This is a test email from Company IT Support, Please ignore this." -Attachments "$tempFile" -dno onSuccess, onFailure -smtpServer $PSEmailServer
Remove-Item -Force $tempFile

#############################
# - Let the user know you sent the email
#############################

Write-Host "`n`nAn email has been sent from $sendingemail to $recieveemail`n" -Fore Yellow;
Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
# - Break The Loop
default { $global:xExitSession=$true;break }
}
}

LoadMenuSystem
If ($xExitSession){
exit-pssession&nbsp;&nbsp;&nbsp; # - Quit Program
}else{
.\SMTPTester.ps1&nbsp;&nbsp;&nbsp; # - Loop To Top
}
</pre>