无法使用Delphi从Gmail帐户下载/检索电子邮件(SSL3_GET_RECORD:错误的版本号)

时间:2016-09-19 09:54:27

标签: email delphi ssl gmail indy

我无法使用Delphi + Indy从Gmail下载/检索电子邮件!

几周以来,我无法阅读Gmail发来的电子邮件。

在下面的代码正常工作之前。

现在,我总是收到此错误消息:

错误:1408F10B:SSL例程:SSL3_GET_RECORD:版本号错误

在连接Gmail期间。

Cryptosense Blog上我读了

  

截至2015年9月,谷歌正在关闭其TLS服务器的SSLv3和RC4支持。

现在有哪些解决方案如何从Gmail阅读电子邮件?

我的代码是这样的:

  1. 启动一个新的Delphi项目

  2. 您必须从此站点下载SSL dll:

  3. http://indy.fulgan.com/SSL/indy_OpenSSL096m.zip

    1. 解压缩文件并将libeay32.dll和ssleay32.dll放在项目路径中。

    2. 将Unit1.pas和Unit1.dfm的代码替换为以下代码

    3. 更改POP3组件的用户名和密码属性,使其与您的GMAIL帐户相匹配。

    4. 运行

    5. // StartOfCode

      unit Unit1;
      
      interface
      
      uses
        Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
        Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
        IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient, IdPOP3,
        IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL;
      
      type
        TForm1 = class(TForm)
          POP3: TIdPOP3;
          Button1: TButton;
          SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
          Memo1: TMemo;
          procedure Button1Click(Sender: TObject);
        private
          { Private declarations }
        public
          { Public declarations }
        end;
      
      var
        Form1: TForm1;
      
      implementation
      
      uses
        IdMessage, IdText;
      
      {$R *.dfm}
      
      procedure TForm1.Button1Click(Sender: TObject);
      var
        lMsg: TIdMessage;
        liCount: Integer;
        liMessages: Integer;
      begin
        POP3.Connect;
        liMessages := POP3.CheckMessages;
        Memo1.Lines.Add('CheckMessages: ' + IntToSTr(liMessages));
        lMsg := TIdMessage.Create;
        try
          POP3.Retrieve(1, lMsg);
          Memo1.Lines.Text := lMsg.MsgId;
          for liCount := 0 to lMsg.MessageParts.Count-1 do
            if lMsg.MessageParts[liCount] is TIdText then
              Memo1.Lines.AddStrings((lMsg.MessageParts[liCount] as TIdText).Body);
        finally
          lMsg.Free;
        end;
      end;
      
      end.
      

      // EndOfCode

      // StartOfDFM

      object Form1: TForm1
        Left = 192
        Top = 114
        Width = 696
        Height = 480
        Caption = 'Form1'
        Color = clBtnFace
        Font.Charset = DEFAULT_CHARSET
        Font.Color = clWindowText
        Font.Height = -11
        Font.Name = 'MS Sans Serif'
        Font.Style = []
        OldCreateOrder = False
        PixelsPerInch = 96
        TextHeight = 13
        object Button1: TButton
          Left = 216
          Top = 16
          Width = 75
          Height = 25
          Caption = 'Button1'
          TabOrder = 0
          OnClick = Button1Click
        end
        object Memo1: TMemo
          Left = 24
          Top = 56
          Width = 657
          Height = 185
          Lines.Strings = (
            'Memo1')
          TabOrder = 1
        end
        object POP3: TIdPOP3
          IOHandler = SSLHandler
          AutoLogin = True
          Host = 'pop.gmail.com'
          Username = 'YourName@gmail.com'
          UseTLS = utUseImplicitTLS
          Password = 'YourPassword'
          Port = 995
          SASLMechanisms = <>
          Left = 40
          Top = 16
        end
        object SSLHandler: TIdSSLIOHandlerSocketOpenSSL
          Destination = 'pop.gmail.com:995'
          Host = 'pop.gmail.com'
          MaxLineAction = maException
          Port = 995
          DefaultPort = 0
          SSLOptions.Method = sslvSSLv3
          SSLOptions.Mode = sslmUnassigned
          SSLOptions.VerifyMode = []
          SSLOptions.VerifyDepth = 0
          Left = 80
          Top = 16
        end
      end
      

      // EndOfDFM

2 个答案:

答案 0 :(得分:2)

以下测试代码适用于我通过POP3登录Gmail:

procedure Test;
var
  IdPOP3: TIdPOP3;
  IdSSL: TIdSSLIOHandlerSocketOpenSSL;
begin
  IdPOP3 := TIdPOP3.Create(nil);
  try
    IdPOP3.Host := 'pop.gmail.com';
    IdPOP3.Port := 995;
    IdPOP3.Username := MyGmailUsername;
    IdPOP3.Password := MyGmailAppPassword;
    IdPOP3.ConnectTimeout := 60000;

    IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(IdPOP3);
    IdSSL.SSLOptions.Method := sslvTLSv1;
    IdSSL.SSLOptions.Mode := sslmClient;
    IdPOP3.IOHandler := IdSSL;
    IdPOP3.UseTLS := utUseImplicitTLS;

    IdPOP3.Connect;
    try
      // use IdPOP3 as needed...
    finally
      IdPOP3.Disconnect;
    end;
  finally
    IdPOP3.Free;
  end;
end;

<强>更新

注意:Indy尚未原生支持OAuth身份验证(但是有第三方实施可供选择),因此如果您想使用真实密码(而不是使用第三方身份验证),那么您需要调整自己的Google帐户安全设置为allow less secure apps以访问您的帐户。但是,如果您的帐户已启用2-Step Verification(并且您应该),则无需允许访问“安全性较低的应用”,您可以生成application-specific password并使用该代替你真正的密码。

答案 1 :(得分:0)

只需将setStateSSLOptions.Method更改为sslvSSLv3