当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 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 脚本。

[玩转系统] 使用 telnet PowerShell 脚本测试 SMTP 连接

在内部 DNS 中,为relay.exoip.com 设置了一条 A 记录,该记录将转换为负载均衡器。如果您只有一台 Exchange Server 或 DNS 循环,则很可能 A 记录指向您的 Exchange Server。

[玩转系统] 使用 telnet PowerShell 脚本测试 SMTP 连接

使用 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.0250 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 您的连接不安全。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯