我可以在转发邮件时将我的服务器SSL证书附加到TIdSMTPRelay组件

时间:2014-10-23 13:19:23

标签: delphi ssl smtp indy smtps

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

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),尽管我发出了STARTTLS命令,这要归功于附件TIdSSLIOHandlerSocketOpenSSLTIdSMTPRelay }。我可以连接到其他邮件服务器的SMTPS端口465,还是TIdSMTPRelay组件只能发送到端口25?有关详细信息,这与雅虎的沟通方式如下:

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:<user229@mail.mydomain.com><EOL>
Recv 23.10.2014 ?. 15:32:16: 250 sender <user229@mail.mydomain.com> ok<EOL>
Sent 23.10.2014 ?. 15:32:16: RCPT TO:<test_user_mail@yahoo.com><EOL>
Recv 23.10.2014 ?. 15:32:16: 250 recipient <test_user_mail@yahoo.com> 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: "user229@mail.mydomain.com" <user229@mail.mydomain.com>   <EOL>Subject: =?UTF-8?B?0JfQsNCz0LvQsNCy0LjQtTIy?=<EOL>To: test_user_mail@yahoo.com<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.

1 个答案:

答案 0 :(得分:2)

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

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

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

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

NoSSL:SMTP会话将不加密。

SupportSSL:SMTP会话将以未加密的方式启动,然后仅在支持时才会将STARTTLS发送到SMTP服务器,否则SMTP会话将继续未加密。如果SSL / TLS握手失败,将引发异常。

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