program SMTP;
{$APPTYPE CONSOLE}
uses
Windows, WinSock;
function EncodeBase64(const inStr: string): string;
function Encode_Byte(b: Byte): char;
const
Base64Code: string[64] =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
begin
Result := Base64Code[(b and $3F)+1];
end;
var
i: Integer;
begin
i := 1;
Result := '';
while i <= Length(InStr) do
begin
Result := Result + Encode_Byte(Byte(inStr[i]) shr 2);
Result := Result + Encode_Byte((Byte(inStr[i]) shl 4) or (Byte(inStr[i+1]) shr 4));
if i+1 <= Length(inStr) then
Result := Result + Encode_Byte((Byte(inStr[i+1]) shl 2) or (Byte(inStr[i+2]) shr 6))
else
Result := Result + '=';
if i+2 <= Length(inStr) then
Result := Result + Encode_Byte(Byte(inStr[i+2]))
else
Result := Result + '=';
Inc(i, 3);
end;
end;
var
MyLogin, MyPass, DestMail, MailText, MailSub: String;
WSA: TWSAData;
S: TSocket;
Addr: TSockAddr;
SendBuf: String;
RecvBuf: array[0..255] of char;
begin
MyLogin:='uwner';
MyPass:='pass';
DestMail:='
[email protected]';
MailSub:='By gravitas';
MailText:='Test mail';
WSAStartup($101, WSA);
Addr.sin_family:=AF_INET;
Addr.sin_port:=htons(25);
Addr.sin_addr.S_addr:=inet_addr('94.100.177.1'); // smtp.mail.ru
S:=Socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
Connect(S, Addr, SizeOf(Addr));
Recv(S, RecvBuf, 255, 0);
SendBuf:='EHLO server'#13#10;
Send(S, SendBuf[1], Length(SendBuf), 0);
Recv(S, RecvBuf, 255, 0);
SendBuf:='AUTH LOGIN'#13#10;
Send(S, SendBuf[1], Length(SendBuf), 0);
Recv(S, RecvBuf, 255, 0);
SendBuf:=EncodeBase64(MyLogin) + #13#10;
Send(S, SendBuf[1], Length(SendBuf), 0);
Recv(S, RecvBuf, 255, 0);
SendBuf:=EncodeBase64(MyPass) + #13#10;
Send(S, SendBuf[1], Length(SendBuf), 0);
Recv(S, RecvBuf, 255, 0);
SendBuf:='MAIL FROM:<' + MyLogin + '@mail.ru>'#13#10;
Send(S, SendBuf[1], Length(SendBuf), 0);
Recv(S, RecvBuf, 255, 0);
SendBuf:='RCPT TO:<' + DestMail + '>'#13#10;
Send(S, SendBuf[1], Length(SendBuf), 0);
Recv(S, RecvBuf, 255, 0);
SendBuf:='DATA'#13#10;
Send(S, SendBuf[1], Length(SendBuf), 0);
Recv(S, RecvBuf, 255, 0);
SendBuf:='From: ' + MyLogin + '@mail.ru'#13#10 +
'To: ' + DestMail + #13#10 +
'Subject: ' + MailSub + #13#10 +
MailText + #13#10 + '.' + #13#10;
Send(S, SendBuf[1], Length(SendBuf), 0);
Recv(S, RecvBuf, 255, 0);
CloseSocket(S);
end.