有谁做过pop3邮件监控程序,快来帮忙!!!!!(200分)

  • 主题发起人 主题发起人 tswhoney
  • 开始时间 开始时间
T

tswhoney

Unregistered / Unconfirmed
GUEST, unregistred user!
我在网上找了一段邮件监控程序,可运行到function TForm1.Socket_Readline(sockfd:Integer):String;
var S:String; buf:array[0..1]of Char;
n:Cardinal;
begin
buf[0]:= #0;buf[1]:=#0; S:='';
n:=recv(sockfd,Buf,1,0);
while n>0 do begin
buf[1]:=#0;
S:=S+buf;
if (buf[0]=#10) then Break;
n:=recv(sockfd,buf,1,0);
end;
Result:=Trim(S);
end;
就出现了死循环,不知是程序哪儿错了,请高手帮忙,源码如下:
unit mail;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
function Connect_Server(host:string;Port:integer):integer;
function Write_Socket(sockfd:Integer; const s:string):Integer;
function Socket_Readline(sockfd:Integer):String;
function Pop3Response(Sockfd:Integer):Bool;
function POP3CheckMail(Email,Password:String;var MailList:TStringList;var ErrorMsg:String):Bool;

{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
function TForm1.Connect_Server(host:string;Port:integer):integer;
var i:integer;
p:^LongInt;
phe:pHostEnt;
sin:sockaddr_in;
begin
sin.sin_family:=AF_INET;
sin.sin_port:=htons(Port);
//Get the IP for host, allowing for dotted decimal
phe:=gethostbyname(pchar(host));
if phe<>nil
then begin
p:=Pointer(phe^.h_addr_list^);
sin.sin_addr.s_addr:=p^;
end
else begin
i:=inet_addr(PChar(Host));
if i<>-1 then
sin.sin_addr.S_addr:=i
end;
//create a socket
Result:=socket(PF_INET,SOCK_STREAM,0);
if (Result=INVALID_SOCKET) then Exit;
//connect to server
if Connect(Result,sin,sizeof(sin))=SOCKET_ERROR
then begin {Error handling} end;
end;

function TForm1.Write_Socket(sockfd:Integer; const s:string):Integer;
begin
result:=Winsock.send(sockfd,pointer(s)^,Length(s),0)

end;

function TForm1.Socket_Readline(sockfd:Integer):String;
var S:String; buf:array[0..1]of Char;
n:Cardinal;
begin
buf[0]:= #0;buf[1]:=#0; S:='';
n:=recv(sockfd,Buf,1,0);
while n>0 do begin
buf[1]:=#0;
S:=S+buf;
if (buf[0]=#10) then Break;
n:=recv(sockfd,buf,1,0);
end;
Result:=Trim(S);
end;

function TForm1.Pop3Response(Sockfd:Integer):Bool;
var
S: string;
begin
S:=socket_readline(sockfd);
if copy(s,1,3)='+OK' then Result:=True
else {if copy(s,1,4)=‘ -ERR' then }Result:=False;
end;

function TForm1.POP3CheckMail(Email,Password:String;var MailList:TStringList;var ErrorMsg:String):Bool;
var sockfd,i:integer;
S,Host,User:String;
begin
Result:=False; ErrorMsg:='';
if MailList=nil then Exit;
S:=Trim(Email);
i:=Pos('@',Email);
User:=Trim(Copy(S,1,i-1));
Host:=Trim(Copy(S,i+1,Length(Email)-i));
MailList.Clear;
if (user='')or(host='') then begin
ErrorMsg:='Invalid email address.';exit; end;
if (Host[1]='[')and (Host[Length(host)]=']')
then begin
Host[1]:=' ';
Host[Length(host)]:=#0;
end;
Host:=Trim(host);
sockfd:=Connect_Server(Host,110);
if not Pop3Response(sockfd)then begin
ErrorMsg:='Cannot connect to server';exit; end;
Write_Socket(sockfd,'USER'+User+#13#10);
IF NOT POP3Response(sockfd) then begin
ErrorMsg:='USER failed'; Exit;end;
Write_Socket(sockfd,'PASS' +Password+#13+#10);
IF NOT POP3Response(sockfd) then begin
ErrorMsg:='PASS failed'; Exit;end;
Write_Socket(sockfd,'LIST'#13#10);
POP3Response(sockfd);
while true do begin
s:=Socket_readline(sockfd);
if s='.' then BREAK;
MailList.Add(S);
end;
Write_Socket(sockfd,'QUIT'#13#10);
Closesocket(sockfd);
Result:=True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var MailList:TstringList;
ErrorMsg:String;
begin
MailList:=TstringList.Create;
POP3CheckMail('ptsw@km169.net','19741015',MailList,ErrorMsg);
If MailList.Count>0 then
MessageBox(0,Pchar('You have'+IntToStr(MailList.Count)+'new messages!'),'New Message!', MB_ICONINFORMATION)
Else if ErrorMsg='' then
MessageBox(0,'No message!','',0)
Else MessageBox(0,Pchar(ErrorMsg),'Error',0);
MailList.Free;
end;
end.
 
用indy的POP3
 
后退
顶部