使用Indy组件delphi xe2 SSL协商发送电子邮件失败 [英] Send email using indy component delphi xe2 SSL negotiation faild
问题描述
我尝试使用Indy组件在XE2中发送电子邮件,并且在我编译项目的笔记本电脑上工作正常。
I tried with Indy component to send email in XE2, and it works fine on my laptop that I compiled my project on.
但是如果我使用exe项目到另一台PC,它显示一条错误消息
But if I take my exe project to another PC, it shows an error message
连接已正常关闭
Connection closed gracefully
或者有时候我得到
SSL协商失败
SSL Negotiation failed
实际上我尝试过很多次来解决问题,但是我做不到。
Actually I tried many times to solve my problem, but I can't.
这是我的代码-我的错误在哪里?我需要一个切实可行的解决方案。
This is my code - where is my mistake? I need a practical solution.
procedure Gmail(username, password, totarget, subject, body :string);
var
DATA : TIdMessage;
SMTP : TIdSMTP;
SSL : TIdSSLIOHandlerSocketOpenSSL;
result:Boolean;
begin
try
SMTP := TIdSMTP.Create(nil);
DATA := TIdMessage.Create(nil);
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
SSL.Destination := 'smtp.gmail.com:587';
SSL.Host := 'smtp.gmail.com';
// SSL.MaxLineAction.maException;
SSL.Port:= 587;
SSL.SSLOptions.Method := sslvTLSv1;
SSL.SSLOptions.Mode := sslmUnassigned;
SSL.SSLOptions.VerifyMode := [];
SSL.SSLOptions.VerifyDepth := 0;
DATA.From.Address := username;
DATA.Recipients.EMailAddresses := totarget;
DATA.Subject := subject;
DATA.Body.Text := body;
if FileExists('D:\Test1.txt') then
TIdAttachmentFile.Create(DATA.MessageParts, 'D:\Test1.txt');
SMTP.IOHandler := SSL;
SMTP.Host := 'smtp.live.com';
SMTP.Port := 587 ;
SMTP.Username := username;
SMTP.Password := password;
// SMTP.SASLMechanisms;
SMTP.UseTLS := utUseExplicitTLS;
try
try
SMTP.Connect;
SMTP.Send(DATA);
Result := True;
except
on E:Exception do
begin
ShowMessage('Cannot send E-Mail: ' + E.Message);
Result := False;
end;
end;
finally
if SMTP.Connected then SMTP.Disconnect;
end;
except
on E : Exception do
begin
ShowMessage('Error in the function SendEmailDelphi: ' + E.Message);
Result := False;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled:= True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
mail_username := 'email@gmail.com';
mail_password := 'pass';
mail_to := 'email@gmail.com';
mail_subject := 'Text email from Delphi project';
mail_body := 'this is a test email sent from Delphi project, do not reply';
try
begin
Gmail(mail_username, mail_password, mail_to , mail_subject, mail_body);
end;
finally
end;
end;
推荐答案
请勿设置 SSL .Destination
, SSL.Host
或 SSL.Port
属性! TIdTCPClient.Connect()
为您处理。此外,您是否认为将 SSL.Destination / Host
设置为 smtp.gmail.com
很奇怪但是将 SMTP.Host
设置为 smtp.live.com
呢? Gmail和Live不是同一服务提供商。
DO NOT set the SSL.Destination
, SSL.Host
, or SSL.Port
properties manually! TIdTCPClient.Connect()
handles that for you. Besides, don't you think it's odd that you are setting SSL.Destination/Host
to smtp.gmail.com
but are setting SMTP.Host
to smtp.live.com
instead? Gmail and Live are not the same service provider.
此外, SSL.SSLOptions.Mode
应该设置为 sslmClient
而不是 sslmUnassigned
。不太重要, TIdSSLIOHandlerSocketOpenSSL
将在配置连接时将其翻转。但是,您还是应该这样做,因为您知道您的代码正在充当客户端。
Also, SSL.SSLOptions.Mode
should be set to sslmClient
instead of sslmUnassigned
. Not too important, TIdSSLIOHandlerSocketOpenSSL
will simply flip it when it configures the connection. But you should do it anyway, since you know your code is acting as a client.
最后,尝试设置 SMTP.UseTLS
之前设置 SMTP.Port
,设置为 UseTLS
更改 Port
,因此您要确保确实连接到期望的正确端口。
And lastly, try setting SMTP.UseTLS
before setting SMTP.Port
, as setting UseTLS
may change the Port
, so you want to make sure you are really connecting to the correct port you are expecting.
话虽如此, SSL协商失败
错误表示TLS握手已启动,但在协商过程中失败。尝试将处理程序分配给 TIdSSLIOHandlerSocketOpenSSL
的 OnStatusInfo / Ex
事件,以查看握手的实际距离。并且,如果您使用的是Indy 10的相对较新版本,请尝试查看引发的异常的 InnerException
属性,它可能会为您提供<$之前出错的线索。随后引发了c $ c> EIdTLSClientTLSHandShakeFailed 异常。
With that said, the SSL Negotiation failed
error means the TLS handshake was started but failed part-way through the negotiation. Try assigning handlers to TIdSSLIOHandlerSocketOpenSSL
's OnStatusInfo/Ex
events to see how far the handshake is actually getting. And if you are using a relatively modern version of Indy 10, try looking at the raised exception's InnerException
property, it might give you a clue as to what went wrong before the EIdTLSClientTLSHandShakeFailed
exception was raised afterwards.
这篇关于使用Indy组件delphi xe2 SSL协商发送电子邮件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!