Отправка e-mail на WinSock API c аттачем [Delphi]
Собсно вопрос, что не так, в каком месте ошибка...
Код:
program Project2;
uses
Windows,
WinSock,
Dialogs,
SysUtils;
const
TimeOut = 10;
type
TAByte = array [0..maxInt-1] of byte;
TPAByte = ^TAByte;
TARR = array of string;
const
CtrlF = #13#10;
var
WSA:TWSAData;
MailSocket:TSocket;
SMTPServer:TSockAddr;
k,r:integer;
mBody:TARR;
s:string;
const
b64 : array [0..63] of char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
function B64Encode(data:string) : string; overload;
var
ic,len : integer;
pi, po : TPAByte;
c1 : dword;
begin
len:=length(data);
if len > 0 then
begin
SetLength(result, ((len + 2) div 3) * 4);
pi := pointer(data);
po := pointer(result);
for ic := 1 to len div 3 do
begin
c1 := pi^[0] shl 16 + pi^[1] shl 8 + pi^[2];
po^[0] := byte(b64[(c1 shr 18) and $3f]);
po^[1] := byte(b64[(c1 shr 12) and $3f]);
po^[2] := byte(b64[(c1 shr 6) and $3f]);
po^[3] := byte(b64[(c1 ) and $3f]);
inc(dword(po), 4);
inc(dword(pi), 3);
end;
case len mod 3 of
1:
begin
c1 := pi^[0] shl 16;
po^[0] := byte(b64[(c1 shr 18) and $3f]);
po^[1] := byte(b64[(c1 shr 12) and $3f]);
po^[2] := byte('=');
po^[3] := byte('=');
end;
2 :
begin
c1 := pi^[0] shl 16 + pi^[1] shl 8;
po^[0] := byte(b64[(c1 shr 18) and $3f]);
po^[1] := byte(b64[(c1 shr 12) and $3f]);
po^[2] := byte(b64[(c1 shr 6) and $3f]);
po^[3] := byte('=');
end;
end;
end
else
result := '';
end;
function _Server(host: string): TInAddr;
var
HostEnt: PHostEnt;
InAddr: TInAddr;
begin
HostEnt := gethostbyname(PChar(host));
FillChar(InAddr, SizeOf(InAddr), 0);
if HostEnt <> nil then
begin
with InAddr, HostEnt^ do
begin
S_un_b.s_b1 := h_addr^[0];
S_un_b.s_b2 := h_addr^[1];
S_un_b.s_b3 := h_addr^[2];
S_un_b.s_b4 := h_addr^[3];
end;
end;
Result := InAddr;
end;
function SMTPRecvReply(MailSocket:TSocket):string;
var
t: integer;
Buffer:Array[0..255] of char;
begin
ZeroMemory(@Buffer,256);
t:=GetTickCount;
repeat
until
(Recv(MailSocket,Buffer,SizeOf(Buffer),0)>0)or(GetTickCount - t >= TimeOut);
result := buffer+CTRLF;
end;
procedure SMTPSendString(MailSocket:TSocket;Str:string);
var
Buffer:Array[0..255] of char;
s:Array[0..255] of string;
begin
inc(k);
StrPCopy(Buffer,Str);
Send(MailSocket,Buffer,length(Str),0);
s[k]:=SMTPRecvReply(MailSocket);
end;
procedure Base64Send(FileName: string);
var
afile: File;
i: longint;
quads: integer;
b: array[0..2279] of byte;
j,k,l,m:integer;
stream:string[76];
begin
AssignFile(afile,filename);
Reset(afile,1);
SMTPSendString(mailsocket, '--========sdfgsagg======'+ CTRLF);
SMTPSendString(mailsocket, 'Content-Type: application/octet-stream; name="'+ExtractFileName(FileName)+'"'+ CTRLF);
SMTPSendString(mailsocket, 'Content-Transfer-Encoding: base64'+ CTRLF);
SMTPSendString(mailsocket, 'Content-Disposition: attachment; filename="'+ExtractFileName(FileName)+'"'+ CTRLF);
SMTPSendString(mailsocket, 'Content-Description: attachment'+ CTRLF);
SMTPSendString(mailsocket, ''+ CTRLF);
stream:='';
quads:=0;
j:=Filesize(afile) div 2280;
for i:=1 to j do
begin
BlockRead(afile,b,2280);
for m:=0 to 39 do
begin
for k:=0 to 18 do
begin
l:=57*m+3*k;
stream[quads+1]:=b64[(b[l]div 4)+1];
stream[quads+2]:=b64[(b[l] mod 4)*16 +(b[l+1] div 16)+1];
stream[quads+3]:=b64[(b[l+1] mod 16)*4 +(b[l+2] div 64)+1];
stream[quads+4]:=b64[b[l+2] mod 64+1];
Inc(quads,4);
if quads=76 then
begin
stream[0]:=#76;
SMTPSendString(mailsocket, stream);
quads:=0;
end;
end;
end;
end;
j:=(Filesize(afile) mod 2280) div 3;
for i:=1 to j do
begin
BlockRead(afile,b,3);
stream[quads+1]:=b64[(b[0]div 4)+1];
stream[quads+2]:=b64[(b[0] mod 4)*16 +(b[1] div 16)+1];
stream[quads+3]:=b64[(b[1] mod 16)*4 +(b[2] div 64)+1];
stream[quads+4]:=b64[b[2] mod 64+1];
Inc(quads,4);
if quads=76 then
begin
stream[0]:=#76;
SMTPSendString(mailsocket, stream);
quads:=0;
end;
end;
if (Filesize(afile) mod 3) = 2 then
begin
BlockRead(afile,b,2);
stream[quads+1]:=b64[(b[0]div 4)+1];
stream[quads+2]:=b64[(b[0] mod 4)*16 +(b[1] div 16)+1];
stream[quads+3]:=b64[(b[1] mod 16)*4 +1];
stream[quads+4]:='=';
Inc(quads,4);
end;
if (Filesize(afile) mod 3) = 1 then
begin
BlockRead(afile,b,1);
stream[quads+1]:=b64[(b[0]div 4)+1];
stream[quads+2]:=b64[(b[0] mod 4)*16 +1];
stream[quads+3]:='=';
stream[quads+4]:='=';
Inc(quads,4);
end;
stream[0]:=Chr(quads);
if quads>0 then
SMTPSendString(mailsocket, stream);
CloseFile(afile);
end;
function StrPCopy(Dest: PChar; const Source: string): PChar;
begin
Result := StrLCopy(Dest, PChar(Source), Length(Source));
end;
function Mailik(smtp:string;port:integer;login:string;pass:string;from:string;dest:string;FileName: string):boolean;
var
i: integer;
begin
WSAStartup(MAKEWORD(1,0),WSA);
MailSocket:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP );
ZeroMemory(@SMTPServer,SizeOf(SMTPServer));
SMTPServer.sin_family:=AF_INET;
SMTPServer.sin_port:=htons(25);
SMTPServer.sin_addr:=_Server(smtp);
if Connect(MailSocket,SMTPServer,SizeOf(SMTPServer))= 0 then
begin
SMTPSendString(mailsocket,'EHLO server '+ CTRLF);
SMTPSendString(mailsocket,'AUTH LOGIN ' + CTRLF + b64encode(login) + CTRLF + b64encode(pass) + CTRLF);
SMTPSendString(mailsocket,'MAIL FROM:' + from + CTRLF + 'RCPT TO:' + dest + CTRLF);
SMTPSendString(mailsocket,'DATA' + CTRLF);
SMTPSendString(mailsocket, 'TESTING7777hello'+ CTRLF);
SMTPSendString(mailsocket, '----------dasdsadsui--------'+ CTRLF);
Base64Send(FileName);
SMTPSendString(mailsocket, '----------dasdsadsui--------'+ CTRLF);
SMTPSendString(mailsocket,''+ CTRLF);
SMTPSendString(mailsocket,'.'+CTRLF+'QUIT'+CTRLF);
end;
CloseSocket(MailSocket);
WSACleanup;
end;
begin
mailik('smtp.inbox.ru',25,'prog','xxxxxx','progr@inbox .ru','kod@xaker.ru','C:\WinLog.txt');
end.
|