我在IdPOP3的UserName和Password中输入错误信息,为什么还是可以顺利的Connected。难道Connect时不检测UserName和Password。
但在CheckMessages时居然返回的是字符串????????????????????????????

解决方案 »

  1.   

    Connect只不过是连接POP3的服务器
    但CheckMessages必须要有UserName和Password登录POP3的服务器,CheckMessages其实是IdPOP3封装好的方法,它的最原此的代码中应该是返回错误码,例如220等等。
      

  2.   

    Connect只不过是连接POP3的服务器,所以不需要UserName和Password。
    但CheckMessages必须要有UserName和Password登录POP3的服务器,CheckMessages其实是IdPOP3封装好的方法,它的最原此的代码中应该是返回错误码,例如220等等。
      

  3.   

    那么怎样在Connected后检查Username和Password正确
      

  4.   

    我查看了Indy的源代码,发现它的Connect方法中封装了Connect, 和登录的功能,也就是说,你在Connect之前设置了正确的Username和Password正确就可以了。然后你可以用CheckMessages来检查它的返回信息,如果Username和Password不正确的话,它会返回相应的错误信息。下面是Indy的Pop3功能的源代码:unit IdPOP3;interfaceuses
      Classes, IdGlobal,
      IdMessage, IdMessageClient;type
      TIdPOP3 = class(TIdMessageClient)
      protected
        FPassword: string;
        FUserId: string;
      public
        function RetrieveRaw(const MsgNum: Integer; const Dest: TStrings): boolean;
        function CheckMessages: longint;
        procedure Connect; override;
        constructor Create(AOwner: TComponent); override;
        function Delete(const MsgNum: Integer): Boolean;
        procedure Disconnect; override;
        procedure KeepAlive;
        function Reset: Boolean;
        function Retrieve(const MsgNum: Integer; AMsg: TIdMessage): Boolean;
        function RetrieveHeader(const MsgNum: Integer; AMsg: TIdMessage): Boolean;
        function RetrieveMsgSize(const MsgNum: Integer): Integer;
        function RetrieveMailBoxSize: integer;
      published
        property Password: string read FPassword write FPassword;
        property UserId: string read FUserId write FUserId;
        property Port default IdPORT_POP3;
      end;implementationuses
      IdTCPConnection,
      SysUtils;function TIdPOP3.CheckMessages: longint;
    var
      Value1, Value2: string;
    begin
      Result := 0;
      SendCmd('STAT', wsOk); {Do not localize}
      Value1 := CmdResult;
      if Value1 <> '' then
      begin
        Value2 := Copy(Value1, 5, Length(Value1) - 5);
        Result := StrToInt(Copy(Value2, 1, IndyPos(' ', Value2) - 1));
      end;
    end;procedure TIdPOP3.Connect;
    begin
      inherited
        Connect;
      try
        GetResponse([wsOk]);
        SendCmd('USER ' + UserId, wsOk); {Do not localize}
        SendCmd('PASS ' + Password, wsOk); {Do not localize}
      except
        Disconnect;
        raise;
      end;
    end;constructor TIdPOP3.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      Port := IdPORT_POP3;
    end;function TIdPOP3.Delete(const MsgNum: Integer): Boolean;
    begin
      SendCmd('DELE ' + IntToStr(MsgNum), wsOk); {Do not localize}
      Result := ResultNo = wsOk;
    end;procedure TIdPOP3.Disconnect;
    begin
      try
        WriteLn('Quit'); {Do not localize}
      finally
        inherited;
      end;
    end;procedure TIdPOP3.KeepAlive;
    begin
      SendCmd('NOOP', wsOk); {Do not localize}
    end;function TIdPOP3.Reset: Boolean;
    begin
      SendCmd('RSET', wsOK); {Do not localize}
      Result := ResultNo = wsOK;
    end;function TIdPOP3.RetrieveRaw(const MsgNum: Integer; const Dest: TStrings):
      boolean;
    begin
      result := SendCmd('RETR ' + IntToStr(MsgNum)) = wsOk; {Do not localize}
      if result then
      begin
        Capture(Dest);
        result := true;
      end;
    end;function TIdPOP3.Retrieve(const MsgNum: Integer;
      AMsg: TIdMessage): Boolean;
    begin  if SendCmd('RETR ' + IntToStr(MsgNum)) = wsOk then {Do not localize}
      begin
        ReceiveHeader(AMsg, '');
        ReceiveBody(AMsg);
      end;
      Result := ResultNo = wsOk;
    end;function TIdPOP3.RetrieveHeader(const MsgNum: Integer;
      AMsg: TIdMessage): Boolean;
    var
      Dummy: string;
    begin
      try
        SendCmd('TOP ' + IntToStr(MsgNum) + ' 0', wsOk); {Do not localize}    ReceiveHeader(AMsg, '');
        Dummy := ReadLn;
        while Dummy = '' do
        begin
          Dummy := ReadLn;
        end;
        Result := Dummy = '.';
      except
        Result := False;
      end;
    end;function TIdPOP3.RetrieveMailBoxSize: integer;
    var
      CurrentLine: string;
    begin
      Result := 0;
      try
        SendCmd('LIST', wsOk); {Do not localize}
        CurrentLine := ReadLn;
        while (CurrentLine <> '.') and (CurrentLine <> '') do {Do not localize}
        begin
          CurrentLine := Copy(CurrentLine, IndyPos(' ', CurrentLine) + 1,
            Length(CurrentLine) - IndyPos(' ', CurrentLine) + 1);
          Result := Result + StrToIntDef(CurrentLine, 0);
          CurrentLine := ReadLn;
        end;
      except
        Result := -1;
      end;
    end;function TIdPOP3.RetrieveMsgSize(const MsgNum: Integer): Integer;
    var
      ReturnResult: string;
    begin
      try
        SendCmd('LIST ' + IntToStr(MsgNum), wsOk); {Do not localize}
        if CmdResult <> '' then
        begin
          ReturnResult := Copy(CmdResult, 5, Length(CmdResult) - 4);
          Result := StrToIntDef(Copy(ReturnResult, IndyPos(' ', ReturnResult) + 1,
            Length(ReturnResult) - IndyPos(' ', ReturnResult) + 1), -1);
        end
        else
          Result := -1;
      except
        Result := -1;
      end;
    end;end.