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

我只是一个虾纸丫 提交于 2019-12-07 18:06:34

问题


Tips i followed is found here.

I do have libeay32.dll and ssleay32.dll in win32 folder.

dfm file:

object tidSMTP: TIdSMTP
    IOHandler = tidSMTP_SSL
    SASLMechanisms = <>
    UseTLS = utUseExplicitTLS
  end
  object tidSMTP_SSL: TIdSSLIOHandlerSocketOpenSSL
    Destination = 'smtp.gmail.com:587'
    Host = 'smtp.gmail.com'
    MaxLineAction = maException
    Port = 587
    DefaultPort = 0
    SSLOptions.Mode = sslmUnassigned
    SSLOptions.VerifyMode = []
    SSLOptions.VerifyDepth = 0
  end

and Send button click event:

procedure TForm1.btnSendClick(Sender: TObject);
var
  mes:TIdMessage;
  fromAddress:TIdEmailAddressItem;
  toAddress:TIdEMailAddressItem;
begin
  tidSMTP.Username := txtUsername.Text;
  tidSMTP.Password := txtPassword.Text;
  tidSMTP.Host := txtSMTPserver.Text;           //smtp.gmail.com
  tidSMTP.Port := StrToInt(txtSMTPport.Text);   //587

  fromAddress := TIdEMailAddressItem.Create;
  fromAddress.Address := txtUsername.Text;

  toAddress := TIdEMailAddressItem.Create;
  toAddress.Address := txtTo.Text;

  mes := TIdMessage.Create;
  mes.ContentType := 'text/plain';
  mes.From := fromAddress;
  mes.ReceiptRecipient := toAddress;
  mes.Subject := txtSubject.Text;

  mes.Body := memoText.Lines;

  tidSMTP.Connect;
  tidSMTP.Send(mes);
  tidSMTP.Disconnect;
end;

Any help would be appreciated!


回答1:


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




回答2:


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...



来源:https://stackoverflow.com/questions/5618310/sending-message-to-gmail-fails-with-start-ssl-negotiation-command-failed-erro

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