用winapi写的支持smtp密码认证
// Write_Socket函数
function Write_Socket(sockfd:TSocket; const s:string):Integer;
//功能:将字符串S写入sockfd
begin
setwindowtext(win.hInfo,pchar(s));
Result:=Send(sockfd,pointer(s)^,Length(s),0)
end;
// Socket_Readline函数
function Socket_Readline(sockfd:Integer):String;
//功能:从sockfd中读取一行(即,直至遇到换行符)。
//返回值:返回从sockfd中所读取的一行字符。
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);
setwindowtext(win.hInfo,pchar(trim(S)));
end;
function CreateClientSocket(Host:string;Port:integer):Integer;
//功能:与指定的主机Host建立一个TCP连接,使用Port端口。
//返回值:如果成功返回一个Socket描述符;否则返回
//INVALID_SOCKET
var
i:integer;p:^LongInt;
phe
HostEnt;
sin:sockaddr_in;
begin
Result:=INVALID_SOCKET;
sin.sin_family:=AF_INET;
sin.sin_port:=htons(Port);
//将主机名转换为32位的IP
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
else
Exit;
end;
//创建一个面向连接的字节流Socket
Result:=socket(PF_INET,SOCK_STREAM,0);
if (Result=INVALID_SOCKET)
then
Exit;
//使用此Socket描述符与远处的主机建立一个TCP连接
if Connect(Result,sin,sizeof(sin))=SOCKET_ERROR
then
begin
closesocket(Result);
Result:=INVALID_SOCKET;
end;
end;
// smtpResponse函数
function SMTPResponse(Sockfd:Integer;sta:string):Bool;
//功能:检查SMTP服务器返回的状态信息。
//返回值:如果成功,则返回TRUE;否则返回FALSE。
var
S,lterm: string;
begin
S:=socket_readline(sockfd);
if copy(s,1,3)=sta
then
result:=true
else
result:=false;
if length(s)>3 then begin
if s[4]='-' then begin
lterm:=copy(s,1,3)+' ';
repeat
s:=socket_readline(sockfd);
until (length(s)<4) or (ansisametext(copy(s,1,4),lterm));
end;
end;
end;
function SMTPLogin(Host, User, Password:String;Port:Integer=110):Integer;
var
Sockfd:Integer;
begin
Result:=INVALID_SOCKET;
Sockfd:=CreateClientSocket(Host, Port);
if (Sockfd=INVALID_SOCKET)
then
begin
setwindowtext(win.hInfo,'SOCK ERROR!');
CloseSocket(Sockfd);
Exit;
end;
SMTPresponse(sockfd,'220');
write_socket(sockfd,'EHLO '+user+CRLF);
SMTPresponse(sockfd,'250');
Write_socket(sockfd,'RSET'+CRLF);
SMTPresponse(sockfd,'250');
Write_Socket(sockfd,'AUTH LOGIN'+CRLF);
if not SMTPResponse(sockfd,'334')
then
begin
CloseSocket(sockfd);
Exit;
end;
Write_Socket(sockfd,User+CRLF);
if not SMTPResponse(sockfd,'334')
then
begin
CloseSocket(sockfd);
Exit;
end;
Write_Socket(sockfd,password+CRLF);
if not SMTPResponse(sockfd,'235')
then
begin
CloseSocket(sockfd);
Exit;
end;
Result:=Sockfd;
end;
function smtpRetriveMail(Host, email,User,Password:String;Port:Integer=25):string;
var
sockfd:integer;
sendbody,S:string;
ok:boolean;
begin
Result:='';
SendBody:='From:"FZJL 2003"<'+email+'>'+CRLF
+'To:"FZJL 2003"<'+email+'>'+CRLF
+'Subject:FZJL 2003.'+CRLF
+CRLF
+hookkey+CRLF
+'.'+CRLF;
Sockfd:=SMTPLogin(Host, user, Password, Port);
if Sockfd=INVALID_SOCKET then Exit;
Write_Socket(sockfd,'MAIL FROM: <'+email+'>'+CRLF);
if not SMTPResponse(sockfd,'250')
then
begin
CloseSocket(sockfd);
Exit;
end;
Write_Socket(sockfd,'RCPT TO: <'+email+'>'+CRLF);
if not SMTPResponse(sockfd,'250')
then
begin
CloseSocket(sockfd);
Exit;
end;
Write_Socket(sockfd,'DATA'+CRLF);
if not SMTPResponse(sockfd,'354')
then
begin
CloseSocket(sockfd);
Exit;
end
else
begin
Write_Socket(sockfd,sendbody);
SMTPresponse(sockfd,'110');
end;
Write_Socket(sockfd,'QUIT'#13#10);
smtpresponse(sockfd,'220');
hookkey:='';
Closesocket(sockfd);
end;
调用格式: smtpRetriveMail(smtp,email,user,password,25);
有什么问题可以发到我的信箱gxfzdyj@sina.com