使用命令行来测试与 Amazon SES SMTP 接口的连接 - Amazon Simple Email Service

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用命令行来测试与 Amazon SES SMTP 接口的连接

您可以从命令行使用本节中介绍的方法来测试与 Amazon SES SMTP 端点的连接、验证 SMTP 凭证以及解决连接问题。这些过程将使用大多数常见操作系统附带的工具和库。

有关解决 SMTP 连接问题的其他信息,请参阅 Amazon SES SMTP 问题

先决条件

在连接到 Amazon SES SMTP 接口时,您必须提供一组 SMTP 凭证。这些 SMTP 凭证不同于您的标准 AWS 凭证。这两种类型的凭证不可互换。有关获取您的 SMTP 凭证的更多信息,请参阅获取 Amazon SES SMTP 凭证

测试到 Amazon SES SMTP 接口的连接

您可以使用命令行来测试您到 Amazon SES SMTP 接口的连接,而无需验证或发送任何消息。此过程可用于基本连接问题的故障排除。如果测试连接失败,请参阅SMTP 问题

本节包括使用 OpenSSL(大多数 Linux、macOS 和 Unix 发行版中都包含此功能,也适用于 Windows)和中的 Test-NetConnection cmdlet PowerShell (包含在最新版本的 Windows 中)测试连接的过程。

Linux, macOS, or Unix

可通过两种方式使用 OpenSSL 连接到 Amazon SES SMTP 接口:在端口 587 上使用显式 SSL,或在端口 465 上使用隐式 SSL。

使用显式 SSL 连接到 SMTP 接口
  • 在命令行上,输入以下命令来连接到 Amazon SES SMTP 服务器:

    openssl s_client -crlf -quiet -starttls smtp -connect email-smtp.us-west-2.amazonaws.com:587

    在前面的命令中,email-smtp.us-west-2.amazonaws.com替换为您所在 AWS 地区的 Amazon SES SMTP 终端节点的网址。有关更多信息,请参阅 区域和 Amazon SES

    如果连接成功,则将显示类似于以下内容的输出:

    depth=2 C = US, O = Amazon, CN = Amazon Root CA 1 verify return:1 depth=1 C = US, O = Amazon, OU = Server CA 1B, CN = Amazon verify return:1 depth=0 CN = email-smtp.us-west-2.amazonaws.com verify return:1 250 Ok

    连接会在处于不活动状态大约 10 秒后自动关闭。

或者,您可以使用隐式 SSL 通过端口 465 连接到 SMTP 接口。

使用隐式 SSL 连接到 SMTP 接口
  • 在命令行上,输入以下命令来连接到 Amazon SES SMTP 服务器:

    openssl s_client -crlf -quiet -connect email-smtp.us-west-2.amazonaws.com:465

    在前面的命令中,email-smtp.us-west-2.amazonaws.com替换为您所在 AWS 地区的 Amazon SES SMTP 终端节点的网址。有关更多信息,请参阅 区域和 Amazon SES

    如果连接成功,则将显示类似于以下内容的输出:

    depth=2 C = US, O = Amazon, CN = Amazon Root CA 1 verify return:1 depth=1 C = US, O = Amazon, OU = Server CA 1B, CN = Amazon verify return:1 depth=0 CN = email-smtp.us-west-2.amazonaws.com verify return:1 220 email-smtp.amazonaws.com ESMTP SimpleEmailService-d-VCSHDP1YZ A1b2C3d4E5f6G7h8I9j0

    连接会在处于不活动状态大约 10 秒后自动关闭。

PowerShell

你可以使用 T est-NetConnection cmdlet 连接 PowerShell 到 Amazon SES SMTP 服务器。

注意

Test-NetConnection cmdlet 可以确定您的计算机是否能连接到 Amazon SES SMTP 端点。但是,它不会测试您的计算机是否能建立与 SMTP 端点的隐式或显式 SSL 连接。要测试 SSL 连接,您可以安装 OpenSSL for Windows 来发送测试电子邮件。

使用 Test-NetConnection cmdlet 连接到 SMTP 接口
  • 在中 PowerShell,输入以下命令连接到 Amazon SES SMTP 服务器:

    Test-NetConnection -Port 587 -ComputerName email-smtp.us-west-2.amazonaws.com

    在前面的命令中,email-smtp.us-west-2.amazonaws.com替换为您所在 AWS 地区的 Amazon SES SMTP 终端节点的 URL,并587替换为端口号。有关 Amazon SES 中的区域端点的更多信息,请参阅区域和 Amazon SES

    如果连接成功,您会看到与以下示例类似的输出:

    ComputerName : email-smtp.us-west-2.amazonaws.com RemoteAddress : 198.51.100.126 RemotePort : 587 InterfaceAlias : Ethernet SourceAddress : 203.0.113.46 TcpTestSucceeded : True

使用命令行通过 Amazon SES SMTP 接口发送电子邮件

您也可以使用命令行通过 Amazon SES SMTP 接口发送邮件。此过程对于测试 SMTP 凭证以及测试特定收件人能否接收您使用 Amazon SES 发送的消息非常有用。

Linux, macOS, or Unix

当电子邮件发件人连接到 SMTP 服务器时,客户端会发出一组标准请求,而服务器会使用标准响应来回复每个请求。这一系列的请求和响应称为 SMTP 对话。当您使用 OpenSSL 连接到 Amazon SES SMTP 服务器时,服务器需要进行 SMTP 对话。

在使用 OpenSSL 连接到 SMTP 接口时,您必须使用 base64 编码对 SMTP 凭证进行编码。此部分包含使用 base64 对凭证进行编码的过程。

从命令行使用 SMTP 接口发送电子邮件
  1. 在命令行中输入以下内容,并email-smtp.us-west-2.amazonaws.com替换为您的 Amazon SES SMTP 终端节点的 AWS 区域 URL。有关更多信息,请参阅区域和 Amazon SES。 :

    #!/bin/bash # Prompt user to provide following information read -p "Configuration set: " CONFIGSET read -p "Enter SMTP username: " SMTPUsername read -p "Enter SMTP password: " SMTPPassword read -p "Sender email address: " MAILFROM read -p "Receiver email address: " RCPT read -p "Email subject: " SUBJECT read -p "Message to send: " DATA echo # Encode SMTP username and password using base64 EncodedSMTPUsername=$(echo -n "$SMTPUsername" | openssl enc -base64) EncodedSMTPPassword=$(echo -n "$SMTPPassword" | openssl enc -base64) # Construct the email Email="EHLO example.com AUTH LOGIN $EncodedSMTPUsername $EncodedSMTPPassword MAIL FROM: $MAILFROM RCPT TO: $RCPT DATA X-SES-CONFIGURATION-SET: $CONFIGSET From: $MAILFROM To: $RCPT Subject: $SUBJECT $DATA . QUIT" echo "$Email" | openssl s_client -crlf -quiet -starttls smtp -connect email-smtp.us-west-2.amazonaws.com:587
  2. 在提示输入每个变量时,输入您的值。

    • 要通过端口 465 使用隐式 SSL 发送,请使用:

      openssl s_client -crlf -quiet -connect email-smtp.us-west-2.amazonaws.com:465

    如果消息已被 Amazon SES 接受,您将看到与以下示例类似的输出:

    250 Ok 01010160d7de98d8-21e57d9a-JZho-416c-bbe1-8ebaAexample-000000

    250 Ok 后面的数字和文本字符串是电子邮件的邮件 ID。

    注意

    连接会在处于不活动状态大约 10 秒后自动关闭。

PowerShell

你可以使用 Net.Mail。 SmtpClient使用显式 SSL 通过端口 587 发送电子邮件的类。

注意

Net.Mail.SmtpClient 类已正式被弃用,Microsoft 建议您使用第三方库。此代码仅用于测试目的,而不应用于生产工作负载。

PowerShell 使用显式 SSL 发送电子邮件
  1. 在文本编辑器中,创建一个新文件。将以下代码粘贴到该文件中:

    function SendEmail($Server, $Port, $Sender, $Recipient, $Subject, $Body) { $Credentials = [Net.NetworkCredential](Get-Credential) $SMTPClient = New-Object Net.Mail.SmtpClient($Server, $Port) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Credentials.Username, $Credentials.Password); try { Write-Output "Sending message..." $SMTPClient.Send($Sender, $Recipient, $Subject, $Body) Write-Output "Message successfully sent to $($Recipient)" } catch [System.Exception] { Write-Output "An error occurred:" Write-Error $_ } } function SendTestEmail(){ $Server = "email-smtp.us-west-2.amazonaws.com" $Port = 587 $Subject = "Test email sent from Amazon SES" $Body = "This message was sent from Amazon SES using PowerShell (explicit SSL, port 587)." $Sender = "sender@example.com" $Recipient = "recipient@example.com" SendEmail $Server $Port $Sender $Recipient $Subject $Body } SendTestEmail

    完成后,将文件另存为 SendEmail.ps1

  2. 对您在上一步中创建的文件进行以下更改:

    • sender@example.com替换为您要从中发送消息的电子邮件地址。

    • recipient@example.com替换为您要向其发送消息的电子邮件地址。

    • email-smtp.us-west-2.amazonaws.com替换为您所在 AWS 地区的 Amazon SES SMTP 终端节点的网址。有关更多信息,请参阅 区域和 Amazon SES

  3. 在中 PowerShell,输入以下命令:

    .\path\to\SendEmail.ps1

    在前面的命令中,path\to\SendEmail.ps1替换为您在步骤 1 中创建的文件的路径。

  4. 在系统提示时,输入您的 SMTP 用户名和密码。

或者,你可以使用 System.Web.Mail。 SmtpMail使用隐式 SSL 通过端口 465 发送电子邮件的类。

注意

System.Web.Mail.SmtpMail 类已正式被弃用,Microsoft 建议您使用第三方库。此代码仅用于测试目的,而不应用于生产工作负载。

PowerShell 使用隐式 SSL 发送电子邮件
  1. 在文本编辑器中,创建一个新文件。将以下代码粘贴到该文件中:

    [System.Reflection.Assembly]::LoadWithPartialName("System.Web") > $null function SendEmail($Server, $Port, $Sender, $Recipient, $Subject, $Body) { $Credentials = [Net.NetworkCredential](Get-Credential) $mail = New-Object System.Web.Mail.MailMessage $mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", $Server) $mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", $Port) $mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", $true) $mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", $Credentials.UserName) $mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", $Credentials.Password) $mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", $timeout / 1000) $mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2) $mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1) $mail.From = $Sender $mail.To = $Recipient $mail.Subject = $Subject $mail.Body = $Body try { Write-Output "Sending message..." [System.Web.Mail.SmtpMail]::Send($mail) Write-Output "Message successfully sent to $($Recipient)" } catch [System.Exception] { Write-Output "An error occurred:" Write-Error $_ } } function SendTestEmail(){ $Server = "email-smtp.us-west-2.amazonaws.com" $Port = 465 $Subject = "Test email sent from Amazon SES" $Body = "This message was sent from Amazon SES using PowerShell (implicit SSL, port 465)." $Sender = "sender@example.com" $Recipient = "recipient@example.com" SendEmail $Server $Port $Sender $Recipient $Subject $Body } SendTestEmail

    完成后,将文件另存为 SendEmail.ps1

  2. 对您在上一步中创建的文件进行以下更改:

    • sender@example.com替换为您要从中发送消息的电子邮件地址。

    • recipient@example.com替换为您要向其发送消息的电子邮件地址。

    • email-smtp.us-west-2.amazonaws.com替换为您所在 AWS 地区的 Amazon SES SMTP 终端节点的网址。有关更多信息,请参阅 区域和 Amazon SES

  3. 在中 PowerShell,输入以下命令:

    .\path\to\SendEmail.ps1

    在前面的命令中,path\to\SendEmail.ps1替换为您在步骤 1 中创建的文件的路径。

  4. 在系统提示时,输入您的 SMTP 用户名和密码。