向“gmail”发送消息失败,并显示“启动SSL协商命令失败”。错误

时间:2011-04-11 07:47:30

标签: delphi gmail indy

我发现的提示here

我在win32文件夹中有libeay32.dll和ssleay32.dll。

dfm文件:

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

和发送按钮点击事件:

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;

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:4)

将SSL方法设置为SSL版本3(tidSMTP_SSL.SSLOptions.Method)。我认为它默认为SSL版本2,但GMail不支持。

SSLOptions.Method := sslvSSLv3;

修改

您可以通过将事件处理程序分配给IOHandler的OnStatusInfo事​​件来记录SSL状态信息:

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;

也许这会给你一个关于谈判失败的线索。

PS:我在Indy 9.0.0.18上,所以事情可能已经改变了。

编辑2:

如果上面没有帮助,请检查是否没有阻止smtp.gmail.com或端口587的防火墙/防病毒软件

答案 1 :(得分:1)

我成功地让它像这样工作:

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;

我使用相同的TEdit,TMemo,但动态创建Indy组件......