Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2004.03.28;
Скачать: CL | DM;

Вниз

Почта   Найти похожие ветки 

 
SAnta ©   (2004-01-09 11:31) [0]

Кто чем... помогите!
Я пишу почтовый клиент на WinApi. На Delphi все просто - готовые копоненты и док много. А WinApi - дремучий лес. Кто чем может, помогите отправлять и получать почту(без участия п. клиента по умолчанию).


 
shluz ©   (2004-01-09 16:51) [1]

поди-ка вирь решил начеркать :) вот , есть кое-что.. нужно немного доработать, но в принципе работает:
unit SMTP_api;

interface

uses
windows, WinSock;

function SendMail(const Server: string; Port: integer;
                const Login, Password, From, To_, Body: string): boolean;

implementation

{$B-} //Complete Boolean expression evaluation - off

function Base64Encode(const Text: string): string;
const
Base64Charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var
i, TextLen: integer;
a1, a2, a3: byte;
b1, b2, b3, b4: byte;
begin
Result:="";
TextLen:=Length(Text);
i:=1;
while i<=TextLen do
begin
  a1:=ord(Text[i]);
  inc(i);
  b1:=(a1 shr 2);
  b2:=((a1 and $03) shl 4);

  if i<=TextLen then
  begin
    a2:=ord(Text[i]);
    inc(i);
    b2:=b2 or (a2 shr 4);
    b3:=((a2 and $0F) shl 2);

    if i<=TextLen then
    begin
      a3:=ord(Text[i]);
      inc(i);
      b3:=b3 or (a3 shr 6);
      b4:=(a3 and $3F);
    end
    else
    begin
      b4:=64;
    end
  end
  else
  begin
    b3:=64;
    b4:=64;
  end;
  Result:=Result+Base64Charset[b1+1]+Base64Charset[b2+1]+Base64Charset[b3+1]+Base64Charset[b4+1];
end;
end;

var
 WSData: TWSAData;
 Sock: TSocket;
 Addr: TSockAddr;

function SendText(Text: string): boolean;
var
sent: integer;
begin
OutputDebugString(PChar(">>> "+Text));
Result:=false;
Text:=Text+#13#10;
while length(Text)>0 do
begin
  sent:=send(Sock, pointer(Text)^, Length(Text), 0);
  if sent=SOCKET_ERROR then Exit;
  delete(Text, 1, sent);
end;
Result:=true;
end;

function ReceiveResponse(SuccessCode: integer): boolean;
var
ch: char;
line: string;
Code, Err: integer;
Response: string;
begin
Result:=false;
response:="";
repeat
  line:="";
  repeat
    if recv(Sock, ch, 1, 0)>0 then
      line:=line+ch
    else
      Exit;
  until ch=#10;
  response:=response+line;
until line[4]=" ";
OutputDebugString(pchar(response));
val(copy(line, 1, 3), Code, err);
if err<>0 then Exit;
Result:=Code=SuccessCode;
end;

function CreateSocket(const Server: string; Port: integer): boolean;
var
HostEnt: PHOSTENT;
begin
Result:=false;
Addr.sin_family:=AF_INET;
Addr.sin_port:=htons(Port);
HostEnt:=gethostbyname(PChar(Server));
if HostEnt=nil then Exit;
Addr.sin_addr:=PInAddr(HostEnt.h_addr^)^;
//FAddr.sin_addr.S_addr:=inet_addr(PChar(Server)); если ip-address, а не domain name
Sock:=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Result:=Sock<>INVALID_SOCKET;
end;

function Connect: boolean;
begin
Result:=
  (winsock.connect(Sock, Addr, SizeOf(Addr))=0) and
  ReceiveResponse(220);
end;
{
function HELO: boolean;
var
HostName: array[0..255] of char;
begin
GetHostName(HostName, Length(HostName));
Result:=
SendText("HELO "+string(HostName)) and
ReceiveResponse(250);
end;
}
function EHLO(const Login, Password: string): boolean;
var
HostName: array[0..255] of char;
begin
GetHostName(HostName, Length(HostName));
Result:=
SendText("EHLO "+string(HostName)) and
ReceiveResponse(250) and
SendText("AUTH LOGIN") and
ReceiveResponse(334) and //тут должна быть проверка, что нам передали строку "Username:" в Base64, но лень переделывать :)
SendText(Base64Encode(Login)) and
ReceiveResponse(334) and // а здесь - "Password:"
SendText(Base64Encode(Password)) and
ReceiveResponse(235);
end;

function MAIL(from: string): boolean;
begin
Result:=
  SendText("MAIL FROM: "+from) and
  ReceiveResponse(250);
end;

function RCPT(to_: string): boolean;
begin
Result:=
SendText("RCPT TO: "+to_) and
ReceiveResponse(250);
end;

function DATA(body: string): boolean;
begin
Result:=
SendText("DATA") and
ReceiveResponse(354) and
SendText(body) and
SendText(".") and
ReceiveResponse(250);
end;

function QUIT: boolean;
begin
Result:=
SendText("QUIT") and
ReceiveResponse(221);
end;

function SendMail(const Server: string; Port: integer;
                const Login, Password, From, To_, Body: string): boolean;
begin
Result:=false;
if WSAStartup(MakeWord(1,1), WSData)=0 then
begin
  if CreateSocket(Server, Port) then
  begin
    if connect then
    begin
      Result:=
            EHLO(Login, Password) and
            MAIL(from) and
            RCPT(to_) and
            DATA(body);
      QUIT;
      shutdown(sock, SD_BOTH);
    end;
    closesocket(Sock);
  end;
  WSACleanup;
end;
end;

end.


 
KILLMAN ©   (2004-01-10 02:27) [2]

Читал, что есть такая штука, как WinSock2, там, вроде, всяких примочек больше, чем в Winsock... может и поможет...



Страницы: 1 вся ветка

Текущий архив: 2004.03.28;
Скачать: CL | DM;

Наверх




Память: 0.47 MB
Время: 0.044 c
11-1056642764
belousov
2003-06-26 19:52
2004.03.28
Доступны ли в KOL компон. доступа к БД и соотв. визуальн. комп.


6-1074309872
olegking
2004-01-17 06:24
2004.03.28
Как запустить сетевой файл?


1-1078556683
able
2004-03-06 10:04
2004.03.28
Как сделать нумерацию строк в TRichEdit?


14-1077303187
тихий вовочка
2004-02-20 21:53
2004.03.28
пользовательские запросы к Яndex


11-1057214468
Vitek
2003-07-03 10:41
2004.03.28
Не качается Updater 3.1 ...