Sending message to gmail fails with “Start SSL negotiation command failed.” error

做~自己de王妃 提交于 2019-12-05 18:39:50

Set you SSL Method to SSL version 3 (tidSMTP_SSL.SSLOptions.Method). I think it defaults to SSL version 2, but GMail does not support that.

SSLOptions.Method := sslvSSLv3;

Edit:

You can log the SSL Status info by assigning an eventhandler to the OnStatusInfo event of your IOHandler:

tidSMTP_SSL.OnStatusInfo := DoOnStatusInfo;

proceudre TForm1.DoOnStatusInfo(Msg: string);
begin
  // when running from IDE, message will appear in 
  // EventLog (Ctrl+Alt+V), otherwise, 
  // use DebugViewer.exe
  OutputDebugString(PChar(Msg)); 
end;

Maybe this will give you a clue about the failing negotation.

PS: I'm on Indy 9.0.0.18, so things may have changed for you.

Edit2:

If above does not help, please check if there is not a firewall / antivirus that is blocking smtp.gmail.com or port 587

I successfully make it worked like this:

procedure TForm1.btn2Click(Sender: TObject);
var
  email      : TIdMessage;
  idSMTPGMail: TIdSMTP;
  idSSLGMail : TIdSSLIOHandlerSocketOpenSSL;
begin
  idSSLGMail                   := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  idSSLGMail.SSLOptions.Method := sslvTLSv1;
  idSSLGMail.SSLOptions.Mode   := sslmUnassigned;

  idSMTPGMail                  := TIdSMTP.Create(nil);
  idSMTPGMail.IOHandler        := idSSLGMail;
  idSMTPGMail.UseTLS           := utUseExplicitTLS;

  email                           := TIdMessage.Create(nil);
  email.From.Address              := txtUsername.Text;
  email.Recipients.EMailAddresses := txtTo.Text;
  email.Subject                   := txtSubject.Text;
  email.Body.Text                 := memoText.Text;

  idSMTPGMail.Host     := 'smtp.gmail.com';
  idSMTPGMail.Port     := 587;
  idSMTPGMail.UserName := txtUsername.Text;
  idSMTPGMail.Password := txtPassword.Text;

  idSMTPGMail.Connect;
  idSMTPGMail.Send(email);
  idSMTPGMail.Disconnect;

  email.Free;
  idSSLGMail.Free;
  idSMTPGMail.Free;
  Beep;
end;

I use the same TEdit, TMemo, but dynamically create the Indy components...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!