Delphi:将IMAP连接到gmail帐户

时间:2015-02-09 13:45:05

标签: delphi ssl gmail imap gmail-imap

我尝试将我连接到测试Gmail帐户。 我已经激活了IMAP,停止了我的本地防火墙和防病毒软件。

我正在使用2个Indy组件:

imap:TIdIMAP4;   ssl:TIdSSLIOHandlerSocketOpenSSL;

我做了

imap.Connect();

它连接但很快断开连接。

此处项目(Delphi XE3)包含用户和psw到gmail测试帐户。

http://sqlmis.com/update/testimap.zip

我在TIdSSLIOHandlerSocketOpenSSL组件和TIdIMAP4中尝试了不同的选项但没有成功。

凭据是:

host=imap.gmail.com
user=testeustachioi@gmail.com

和2个indy组件是:

object ssl: TIdSSLIOHandlerSocketOpenSSL
    OnStatus = sslStatus
    Destination = 'imap.gmail.com:993'
    Host = 'imap.gmail.com'
    MaxLineAction = maException
    Port = 993
    DefaultPort = 0
    SSLOptions.Method = sslvSSLv23
    SSLOptions.SSLVersions = [sslvSSLv3, sslvTLSv1]
    SSLOptions.Mode = sslmUnassigned
    SSLOptions.VerifyMode = []
    SSLOptions.VerifyDepth = 0

object imap: TIdIMAP4
    OnStatus = imapStatus
    IOHandler = ssl
    OnDisconnected = imapDisconnected
    OnConnected = imapConnected
    Password = 'fudbafqpmjgikxct'
    Port = 993
    Username = 'testeustachioi@gmail.com'
    Host = 'imap.gmail.com'
    UseTLS = utUseImplicitTLS
    SASLMechanisms = <>
    MilliSecsToWaitToClearBuffer = 10

非常感谢任何帮助或建议

彼得

P.S。:这里是imap和ssl组件的日志

SSL Status = Resolving hostname imap.gmail.com.
SSL Status = Connecting to 64.233.167.108.
SSL Info = SSL status: "before/connect initialization"
SSL Info = SSL status: "before/connect initialization"
SSL Info = SSL status: "SSLv2/v3 write client hello A"
SSL Info = SSL status: "SSLv3 read server hello A"
SSL Info = SSL status: "SSLv3 read server certificate A"
SSL Info = SSL status: "SSLv3 read server key exchange A"
SSL Info = SSL status: "SSLv3 read server done A"
SSL Info = SSL status: "SSLv3 write client key exchange A"
SSL Info = SSL status: "SSLv3 write change cipher spec A"
SSL Info = SSL status: "SSLv3 write finished A"
SSL Info = SSL status: "SSLv3 flush data"
SSL Info = SSL status: "SSLv3 read server session ticket A"
SSL Info = SSL status: "SSLv3 read finished A"
SSL Info = SSL status: "SSL negotiation finished successfully"
SSL Info = SSL status: "SSL negotiation finished successfully"
SSL Info = Cipher: name = ECDHE-RSA-RC4-SHA; description = ECDHE-RSA-RC4-SHA       SSLv3 Kx=ECDH     Au=RSA  Enc=RC4(128)  Mac=SHA1
; bits = 128; version = TLSv1/SSLv3; 
Status = Connected.
IMAP Connected SUCCESS
Status = Disconnecting.
IMAP DISCONNECTED
Status = Disconnected.

这里是完整的单位代码:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdComponent, IdIOHandler,

  IdException,

  IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdIMAP4, IdMessage,
  IdBaseComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
  IdMessageClient, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    mess: TIdMessage;
    imap: TIdIMAP4;
    ssl: TIdSSLIOHandlerSocketOpenSSL;
    mLog: TMemo;
    Button1: TButton;
    lbFolder: TListBox;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure imapConnected(Sender: TObject);
    procedure imapDisconnected(Sender: TObject);
    procedure imapStatus(ASender: TObject; const AStatus: TIdStatus;
      const AStatusText: string);
    procedure sslStatus(ASender: TObject; const AStatus: TIdStatus;
      const AStatusText: string);
    procedure sslStatusInfo(const AMsg: string);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
    sender_mail, sender_name, uid : string;
begin
imap.Connect();
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
imap.ListMailBoxes(lbFolder.Items);
end;

procedure TForm1.imapConnected(Sender: TObject);
begin
mLog.Lines.Add('IMAP Connected SUCCESS');
end;

procedure TForm1.imapDisconnected(Sender: TObject);
begin
mLog.Lines.Add('IMAP DISCONNECTED');
end;

procedure TForm1.imapStatus(ASender: TObject; const AStatus: TIdStatus;
  const AStatusText: string);
begin
mLog.Lines.Add('Status = ' + AStatusText);
end;

procedure TForm1.sslStatus(ASender: TObject; const AStatus: TIdStatus;
  const AStatusText: string);
begin
mLog.Lines.Add('SSL Status = ' + AStatusText);
end;

procedure TForm1.sslStatusInfo(const AMsg: string);
begin
mLog.Lines.Add('SSL Info = ' + AMsg);
end;

end.

1 个答案:

答案 0 :(得分:-1)

我花了很多时间来进行这个imap连接,尝试使用其他组件, 最后,再次重新检查,连接凭据错误(一个字符错误)。现在它起作用了。 很多人抱歉打扰了 彼得