delphi - 中继邮件时可以将服务器 SSL 证书附加到 TIdSMTPRelay 组件吗

标签 delphi ssl smtp indy smtps

我想知道在使用 indy 中的 TIdSMTPRelay 组件中继邮件时是否可以使用我的服务器证书。这就是我的代码中的邮件中继部分的样子:

procedure TMyForm.SMTPServerMsgReceive(ASender: TIdSMTPServerContext; AMsg : TStream; 
  var LAction: TIdDataReply);      
begin
  //The AMsg (TStream) is being transformed to MsgDecode (TIdMessage) and 
  // all relay recipients to RelayRecipients (TIdEMailAddressList) using     
  // [http://stackoverflow.com/questions/8499524/using-indy-smtpserver]

  // SSLRelayHandler is a TIdSSLIOHandlerSocketOpenSsl indy component and SMTPRelay is 
  // a TIdSMTPRelay indy component 
  SMTPRelay.DNSServer := myDNSServer;
  SSLRelayHandler.SSLOptions.Method := sslvSSLv23; 
  SSLRelayHandler.SSLOptions.KeyFile := myMailServerKey;
  SSLRelayHandler.SSLOptions.CertFile := myMailServerCert;
  SSLRelayHandler.SSLOptions.RootCertFile := myMailServerRootCert;
  SMTPRelay.IOHandler := SSLRelayHandler;
  SMTPRelay.SSLOptions.SSLSupport := SupportSSL;
  try
    SMTPRelay.Send(MsgDecode, RelayRecipients); 
  except on e : Exception do 
  end;
end;

有时邮件从未发送,有时虽然已发送但会转到垃圾邮件部分(yahoo),但由于附加的 TIdSSLIOHandlerSocketOpenSSL,我发出了 STARTTLS 命令到我的 TIdSMTPRelay。我可以连接到另一个邮件服务器的 SMTPS 端口 465TIdSMTPRelay 组件只能发送到端口 25 吗?有关更多详细信息,这是与 yahoo 的通信方式:

Stat Connected.
Recv 23.10.2014 ?. 15:32:15: 220 mta1418.mail.gq1.yahoo.com ESMTP ready<EOL>
Sent 23.10.2014 ?. 15:32:15: EHLO mail.mydomain.com<EOL>
Recv 23.10.2014 ?. 15:32:15: 250-mta1418.mail.gq1.yahoo.com<EOL>250-PIPELINING<EOL>250-SIZE      41943040<EOL>250-8BITMIME<EOL>250 STARTTLS<EOL>
Sent 23.10.2014 ?. 15:32:15: STARTTLS<EOL>
Recv 23.10.2014 ?. 15:32:15: 220 Start TLS<EOL>
Sent 23.10.2014 ?. 15:32:16: EHLO mail.mydomain.com<EOL>
Recv 23.10.2014 ?. 15:32:16: 250-mta1418.mail.gq1.yahoo.com<EOL>250-PIPELINING<EOL>250-SIZE 41943040<EOL>250 8BITMIME<EOL>
Sent 23.10.2014 ?. 15:32:16: MAIL FROM:<<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3b6b0a6b1f1f1fa83aea2aaafedaebaa7acaea2aaadeda0acae" rel="noreferrer noopener nofollow">[email protected]</a>><EOL>
Recv 23.10.2014 ?. 15:32:16: 250 sender <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="641117011656565d2409050d084a091d000b09050d0a4a070b09" rel="noreferrer noopener nofollow">[email protected]</a>> ok<EOL>
Sent 23.10.2014 ?. 15:32:16: RCPT TO:<<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8afeeff9fed5fff9eff8d5e7ebe3e6caf3ebe2e5e5a4e9e5e7" rel="noreferrer noopener nofollow">[email protected]</a>><EOL>
Recv 23.10.2014 ?. 15:32:16: 250 recipient <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f581908681aa80869087aa98949c99b58c949d9a9adb969a98" rel="noreferrer noopener nofollow">[email protected]</a>> ok<EOL>
Sent 23.10.2014 ?. 15:32:16: DATA<EOL>
Recv 23.10.2014 ?. 15:32:17: 354 go ahead<EOL>
Sent 23.10.2014 ?. 15:32:17: From: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c696f796e2e2e255c717d75703271657873717d7572327f7371" rel="noreferrer noopener nofollow">[email protected]</a>" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7005031502424249301d11191c5e1d09141f1d11191e5e131f1d" rel="noreferrer noopener nofollow">[email protected]</a>>   <EOL>Subject: =?UTF-8?B?0JfQsNCz0LvQsNCy0LjQtTIy?=<EOL>To: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7c3d2c4c3e8c2c4d2c5e8dad6dedbf7ced6dfd8d899d4d8da" rel="noreferrer noopener nofollow">[email protected]</a><EOL>Date: Thu, 23     Oct 2014 15:32:17 +0300<EOL>
Sent 23.10.2014 ?. 15:32:17: <EOL>
Sent 23.10.2014 ?. 15:32:17: body of the mail here<EOL>
Sent 23.10.2014 ?. 15:32:17: <EOL>
Sent 23.10.2014 ?. 15:32:17: .<EOL>
Recv 23.10.2014 ?. 15:32:18: 250 ok dirdel<EOL>
Sent 23.10.2014 ?. 15:32:18: QUIT<EOL>
Recv 23.10.2014 ?. 15:32:18: 221 mta1418.mail.gq1.yahoo.com<EOL>
Stat Disconnected.

最佳答案

TIdSMTPRelay 可以使用其他端口。您应该将 Port 属性设置为其默认值 (25),以便它可以根据其内部管理 SSL/TLS 连接的方式在 25 和 465 之间切换。

SSLOptions 属性控制 TIdSMTPRelay 如何管理其 SSL/TLS 连接。 SSLOptions.SSLSupport 可以设置为 NoSSLSupportSSLRequireSSLSSLOptions.TryImplicitTLS 可以设置为 True 或 False。请参阅 TIdSMTPRelay.Connect() 中的实现。

SSLOptions.TryImplicitTLS 为 true(端口 465)时,Send() 将在连接时尝试使用 UseTLS=utImplicitTLS。这意味着一旦套接字连接就会启动 SSL/TLS 握手。

如果失败,或者 SSLOptions.TryImplicitTLS 为 false(端口 25 和 587),Send() 将使用 UseTLS=utNoTLSSupportUseTLS=utUseExplicitTLSUseTLS= utUseRequireTLS,具体取决于 SSLOptions.SSLSupport 的值:

NoSSL:SMTP session 将不加密。

SupportSSL:SMTP session 将在未加密的情况下启动,然后只有在支持的情况下 STARTTLS 才会发送到 SMTP 服务器,否则 SMTP session 将继续在未加密的情况下进行。如果 SSL/TLS 握手失败,则会引发异常。

RequireSSL:SMTP session 将以未加密的方式启动,然后 STARTTLS 将被发送到 SMTP 服务器(如果支持),否则 SMTP session 将被关闭并引发异常。如果 SSL/TLS 握手失败,则会引发异常。

关于delphi - 中继邮件时可以将服务器 SSL 证书附加到 TIdSMTPRelay 组件吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26529051/

相关文章:

python - 将标题添加到 Django EmailMultiAlternatives

php - SMTP 服务器响应 : 550 - currently not permitted to relay 550-through this server

delphi - 有条件地防止“确定”按钮关闭对话框

delphi - 如何在 Delphi 中递归创建文件夹?

ssl - 在 Tomcat 6 上设置 HTTPS

PowerShell 使用自签名证书连接到 REST API。

azure - HTTPS 不适用于在具有自定义域的 Service Fabric 上运行的 Kestrel 3.2.187/ASPNET Core 2.1.5

java - 从 java REST 服务发送验证码/链接

delphi - 如果它们只是用作表单控件的指针,我是否需要 'create'/初始化局部变量?

delphi - Parallel.For 和 Parallel.For 之间有区别吗?