[玩转系统] 使用 telnet PowerShell 脚本测试 SMTP 连接
作者:精品下载站 日期:2024-12-14 22:31:58 浏览:13 分类:玩电脑
使用 telnet PowerShell 脚本测试 SMTP 连接
如何使用 PowerShell 测试 SMTP 通信?在防火墙上为特定系统启用 SMTP 服务或配置匿名 SMTP 中继后,您希望检查 SMTP 是否正常工作。 Telnet 是您的选择。但是,如果您可以使用 PowerShell 中包含的 telnet 呢?在本文中,您将了解如何使用 telnet PowerShell 脚本测试 SMTP 通信。
在你开始之前
最好验证一下:
没有防病毒或安全产品阻止 SMTP 连接
没有防火墙阻止 SMTP 连接
下载 SMTP Telnet PowerShell 脚本
下载 Test-SMTP-Telnet.ps1 PowerShell 脚本或将以下代码复制并粘贴到记事本中。将其命名为 Test-SMTP-Telnet.ps1 并将其放置在 C:\scripts 文件夹中。如果没有脚本文件夹,请创建一个。
param (
[Parameter(Mandatory = $true)]
[string]$RemoteHost, # Exchange Server host name or IP address
[Parameter(Mandatory = $true)]
[string]$Port, # Choose port number 25 or any other
[Parameter(Mandatory = $true)]
[string]$From, # Sender email address
[Parameter(Mandatory = $true)]
[string]$To, # Recipient email address
[Parameter(Mandatory = $true)]
[string]$Greet, # Choose between HELO or EHLO
[Parameter(Mandatory = $false)]
[string]$Subject # Subject
)
function readResponse {
while ($stream.DataAvailable) {
$read = $stream.Read($buffer, 0, 1024)
write-host -n -foregroundcolor cyan ($encoding.GetString($buffer, 0, $read))
""
}
}
$socket = new-object System.Net.Sockets.TcpClient($RemoteHost, $Port)
if ($null -eq $socket) { return; }
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter($stream)
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding
readResponse($stream)
write-host -foregroundcolor yellow $Greet
""
$writer.WriteLine($Greet)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "MAIL FROM: $from"
write-host -foregroundcolor yellow "MAIL FROM: $from"
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "RCPT TO: $To"
write-host -foregroundcolor yellow $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
if ($Subject -eq "") {
$command = "QUIT"
write-host -foregroundcolor yellow $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$writer.Close()
$stream.Close()
}
else {
$command = "DATA"
write-host -foregroundcolor yellow $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
write-host -foregroundcolor yellow "Subject : $Subject"
""
$writer.WriteLine("subject:$Subject `r")
$writer.Flush()
start-sleep -m 500
readResponse($stream)
write-host -foregroundcolor yellow "."
""
$writer.WriteLine(".")
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "QUIT"
write-host -foregroundcolor yellow $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$writer.Close()
$stream.Close()
}
准备好 SMTP PowerShell 脚本后,您将使用 EHLO 和 HELO 命令测试 SMTP 连接。
测试 SMTP 连接
您想知道 SMTP 是否正常工作,并且需要测试 SMTP 连接。在此示例中,我们将使用端口 25 测试匿名 SMTP 中继。您可以输入与端口 25 不同的端口并测试您喜欢的端口。
在客户端本身而不是在 Exchange 服务器上运行 SMTP Telnet PowerShell 脚本非常重要。
例如,您将 Windows Server AP01-2016 IP 地址 192.168.1.60 添加到 Exchange Server 的 SMTP 中继。 AP01-2016 服务器是向内部和外部收件人发送电子邮件的应用程序服务器。登录此服务器并运行 Telnet PowerShell 脚本。
在内部 DNS 中,为relay.exoip.com 设置了一条 A 记录,该记录将转换为负载均衡器。如果您只有一台 Exchange Server 或 DNS 循环,则很可能 A 记录指向您的 Exchange Server。
使用 Exchange Server 主机名、IP 地址或 DNS 记录。在此示例中,relay.exoip.com 将解析为 Exchange Server IP 地址之一。
HELO SMTP 命令
在允许发送电子邮件的系统上启动 PowerShell。从 HELO 命令开始。它看起来很棒,因为发件人和收件人响应代码都正常。
PS C:\> PowerShell.exe -ExecutionPolicy Bypass C:\scripts\Test-SMTP-Telnet.ps1 -RemoteHost "relay.exoip.com" -Port "25" -From "[email protected]" -To "[email protected]" -Greet "HELO"
220 EX01-2016.exoip.local Microsoft ESMTP MAIL Service ready at Sat, 19 Dec 2020 18:52:54 +0100
HELO
250 EX01-2016.exoip.local Hello [192.168.1.54]
MAIL FROM: [email protected]
250 2.1.0 Sender OK
RCPT TO: [email protected]
250 2.1.5 Recipient OK
QUIT
221 2.0.0 Service closing transmission channel
下面的输出是它不工作时得到的结果。如您所见,不会有响应代码 250 2.1.5 Recipient OK。检查您的中继接收连接器。
PS C:\> PowerShell.exe -ExecutionPolicy Bypass C:\scripts\Test-SMTP-Telnet.ps1 -RemoteHost "relay.exoip.com" -Port "25" -From "[email protected]" -To "[email protected]" -Greet "HELO"
220 EX01-2016.exoip.local Microsoft ESMTP MAIL Service ready at Sat, 19 Dec 2020 18:55:32 +0100
HELO
250 EX01-2016.exoip.local Hello [192.168.1.54]
MAIL FROM: [email protected]
250 2.1.0 Sender OK
RCPT TO: [email protected]
QUIT
EHLO SMTP 命令
再做一次,但这次是用 EHLO。响应代码250 2.1.0和250 2.1.5表明SMTP连接良好。如果您没有看到这些响应代码,请检查您的 SMTP 配置。
PS C:\> PowerShell.exe -ExecutionPolicy Bypass C:\scripts\Test-SMTP-Telnet.ps1 -RemoteHost "relay.exoip.com" -Port "25" -From "[email protected]" -To "[email protected]" -Greet "EHLO"
220 EX01-2016.exoip.local Microsoft ESMTP MAIL Service ready at Sat, 19 Dec 2020 18:56:22 +0100
EHLO
250-EX01-2016.exoip.local Hello [192.168.1.54]
250-SIZE 37748736
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250 CHUNKING
MAIL FROM: [email protected]
250 2.1.0 Sender OK
RCPT TO: [email protected]
250 2.1.5 Recipient OK
QUIT
221 2.0.0 Service closing transmission channel
SMTP 端口 25 无法访问
如果无法访问端口 25,则会出现以下错误。
PS C:\> PowerShell.exe -ExecutionPolicy Bypass C:\scripts\Test-SMTP-Telnet.ps1 -RemoteHost "relay.exoip.com" -Port "25" -From "[email protected]" -To "[email protected]" -Greet "HELO"
new-object : Exception calling ".ctor" with "2" argument(s): "A connection attempt failed because the connected party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond 192.168.1.54:25"
At C:\scripts\Test-SMTP-Telnet.ps1:29 char:11
+ $socket = new-object System.Net.Sockets.TcpClient($RemoteHost, $Port)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
HELO SMTP 命令并向收件人发送电子邮件
发送电子邮件并验证该邮件是否显示在收件人的收件箱中。使用参数 -主题。
PS C:\> PowerShell.exe -ExecutionPolicy Bypass C:\scripts\Test-SMTP-Telnet.ps1 -RemoteHost "relay.exoip.com" -Port "25" -From "[email protected]" -To "[email protected]" -Greet "HELO" -Subject "Testing"
220 EX01-2016.exoip.local Microsoft ESMTP MAIL Service ready at Sat, 19 Dec 2020 19:00:19 +0100
HELO
250 EX01-2016.exoip.local Hello [192.168.1.54]
MAIL FROM: [email protected]
250 2.1.0 Sender OK
RCPT TO: [email protected]
250 2.1.5 Recipient OK
DATA
354 Start mail input; end with <CRLF>.<CRLF>
Subject : Testing
.
250 2.6.0 <[email protected]> [InternalId=3496103378945, Hostname=EX01-2016.exoip.local] 1519 bytes in 0.131, 11,274 KB/sec Queued mail for delivery
QUIT
221 2.0.0 Service closing transmission channel
如果您没有收到电子邮件,请检查您的垃圾邮件文件夹或垃圾邮件过滤器。
了解更多:使用 Kemp 负载均衡器实现 Exchange SMTP 高可用性 »
结论
在本文中,您学习了如何使用 telnet PowerShell 脚本测试 SMTP 连接。将 STMP Telnet PowerShell 脚本保存在要测试的客户端/服务器上至关重要。填写参数并运行 PowerShell 脚本。
您喜欢这篇文章吗?您可能还喜欢 Exchange Server OWA 您的连接不安全。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag