PageRenderTime 143ms CodeModel.GetById 26ms RepoModel.GetById 8ms app.codeStats 0ms

/units/synapse/smtpsend.pas

http://github.com/rofl0r/KOL
Pascal | 506 lines | 445 code | 35 blank | 26 comment | 88 complexity | 0e095a5d9540a5293d38ea01b5b465e6 MD5 | raw file
  1. {==============================================================================|
  2. | Project : Delphree - Synapse | 002.001.004 |
  3. |==============================================================================|
  4. | Content: SMTP client |
  5. |==============================================================================|
  6. | The contents of this file are Subject to the Mozilla Public License Ver. 1.1 |
  7. | (the "License"); you may not use this file except in compliance with the |
  8. | License. You may obtain a Copy of the License at http://www.mozilla.org/MPL/ |
  9. | |
  10. | Software distributed under the License is distributed on an "AS IS" basis, |
  11. | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
  12. | the specific language governing rights and limitations under the License. |
  13. |==============================================================================|
  14. | The Original Code is Synapse Delphi Library. |
  15. |==============================================================================|
  16. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  17. | Portions created by Lukas Gebauer are Copyright (c) 1999,2000,2001. |
  18. | All Rights Reserved. |
  19. |==============================================================================|
  20. | Contributor(s): |
  21. |==============================================================================|
  22. | History: see HISTORY.HTM from distribution package |
  23. | (Found at URL: http://www.ararat.cz/synapse/) |
  24. |==============================================================================}
  25. {$WEAKPACKAGEUNIT ON}
  26. unit SMTPsend;
  27. interface
  28. uses
  29. KOL,
  30. blcksock, SynaUtil, SynaCode;
  31. const
  32. cSmtpProtocol = 'smtp';
  33. type
  34. PSMTPSend = ^TSMTPSend;
  35. TSMTPSend = object(TObj)
  36. private
  37. FSock: PTCPBlockSocket;
  38. FTimeout: Integer;
  39. FSMTPHost: string;
  40. FSMTPPort: string;
  41. FResultCode: Integer;
  42. FResultString: string;
  43. FFullResult: PStrList;
  44. FESMTPcap: PStrList;
  45. FESMTP: Boolean;
  46. FUsername: string;
  47. FPassword: string;
  48. FAuthDone: Boolean;
  49. FESMTPSize: Boolean;
  50. FMaxSize: Integer;
  51. FEnhCode1: Integer;
  52. FEnhCode2: Integer;
  53. FEnhCode3: Integer;
  54. FSystemName: string;
  55. procedure EnhancedCode(const Value: string);
  56. function ReadResult: Integer;
  57. function AuthLogin: Boolean;
  58. function AuthCram: Boolean;
  59. function Helo: Boolean;
  60. function Ehlo: Boolean;
  61. function Connect: Boolean;
  62. public
  63. destructor Destroy; virtual;
  64. function Login: Boolean;
  65. procedure Logout;
  66. function Reset: Boolean;
  67. function NoOp: Boolean;
  68. function MailFrom(const Value: string; Size: Integer): Boolean;
  69. function MailTo(const Value: string): Boolean;
  70. function MailData(Value: PStrList): Boolean;
  71. function Etrn(const Value: string): Boolean;
  72. function Verify(const Value: string): Boolean;
  73. function EnhCodeString: string;
  74. function FindCap(const Value: string): string;
  75. property Timeout: Integer read FTimeout Write FTimeout;
  76. property SMTPHost: string read FSMTPHost Write FSMTPHost;
  77. property SMTPPort: string read FSMTPPort Write FSMTPPort;
  78. property ResultCode: Integer read FResultCode;
  79. property ResultString: string read FResultString;
  80. property FullResult: PStrList read FFullResult;
  81. property ESMTPcap: PStrList read FESMTPcap;
  82. property ESMTP: Boolean read FESMTP;
  83. property Username: string read FUsername Write FUsername;
  84. property Password: string read FPassword Write FPassword;
  85. property AuthDone: Boolean read FAuthDone;
  86. property ESMTPSize: Boolean read FESMTPSize;
  87. property MaxSize: Integer read FMaxSize;
  88. property EnhCode1: Integer read FEnhCode1;
  89. property EnhCode2: Integer read FEnhCode2;
  90. property EnhCode3: Integer read FEnhCode3;
  91. property SystemName: string read FSystemName Write FSystemName;
  92. property Sock: PTCPBlockSocket read FSock;
  93. end;
  94. function SendToRaw(const MailFrom, MailTo, SMTPHost: string;
  95. MailData: PStrList; const Username, Password: string): Boolean;
  96. function SendTo(const MailFrom, MailTo, Subject, SMTPHost: string;
  97. MailData: PStrList): Boolean;
  98. function SendToEx(const MailFrom, MailTo, Subject, SMTPHost: string;
  99. MailData: PStrList; const Username, Password: string): Boolean;
  100. function NewSMTPSend : PSMTPSend;
  101. implementation
  102. const
  103. CRLF = #13#10;
  104. function NewSMTPSend : PSMTPSend;
  105. begin
  106. New(Result,Create);
  107. Result.FFullResult := NewStrList;//TStringList.Create;
  108. Result.FESMTPcap := NewStrList;//TStringList.Create;
  109. Result.FSock := NewTCPBlockSocket;//.Create;
  110. Result.FSock.CreateSocket;
  111. Result.FTimeout := 300000;
  112. Result.FSMTPhost := cLocalhost;
  113. Result.FSMTPPort := cSmtpProtocol;
  114. Result.FUsername := '';
  115. Result.FPassword := '';
  116. Result.FSystemName := Result.FSock.LocalName;
  117. end;
  118. destructor TSMTPSend.Destroy;
  119. begin
  120. FSock.Free;
  121. FESMTPcap.Free;
  122. FFullResult.Free;
  123. inherited Destroy;
  124. end;
  125. procedure TSMTPSend.EnhancedCode(const Value: string);
  126. var
  127. s, t: string;
  128. e1, e2, e3: Integer;
  129. begin
  130. FEnhCode1 := 0;
  131. FEnhCode2 := 0;
  132. FEnhCode3 := 0;
  133. s := Copy(Value, 5, Length(Value) - 4);
  134. t := SeparateLeft(s, '.');
  135. s := SeparateRight(s, '.');
  136. if t = '' then
  137. Exit;
  138. if Length(t) > 1 then
  139. Exit;
  140. e1 := StrToIntDef(t, 0);
  141. if e1 = 0 then
  142. Exit;
  143. t := SeparateLeft(s, '.');
  144. s := SeparateRight(s, '.');
  145. if t = '' then
  146. Exit;
  147. if Length(t) > 3 then
  148. Exit;
  149. e2 := StrToIntDef(t, 0);
  150. t := SeparateLeft(s, ' ');
  151. if t = '' then
  152. Exit;
  153. if Length(t) > 3 then
  154. Exit;
  155. e3 := StrToIntDef(t, 0);
  156. FEnhCode1 := e1;
  157. FEnhCode2 := e2;
  158. FEnhCode3 := e3;
  159. end;
  160. function TSMTPSend.ReadResult: Integer;
  161. var
  162. s: string;
  163. begin
  164. Result := 0;
  165. FFullResult.Clear;
  166. repeat
  167. s := FSock.RecvString(FTimeout);
  168. FResultString := s;
  169. FFullResult.Add(s);
  170. if FSock.LastError <> 0 then
  171. Break;
  172. until Pos('-', s) <> 4;
  173. s := FFullResult.Items[0];
  174. if Length(s) >= 3 then
  175. Result := StrToIntDef(Copy(s, 1, 3), 0);
  176. FResultCode := Result;
  177. EnhancedCode(s);
  178. end;
  179. function TSMTPSend.AuthLogin: Boolean;
  180. begin
  181. Result := False;
  182. FSock.SendString('AUTH LOGIN' + CRLF);
  183. if ReadResult <> 334 then
  184. Exit;
  185. FSock.SendString(EncodeBase64(FUsername) + CRLF);
  186. if ReadResult <> 334 then
  187. Exit;
  188. FSock.SendString(EncodeBase64(FPassword) + CRLF);
  189. Result := ReadResult = 235;
  190. end;
  191. function TSMTPSend.AuthCram: Boolean;
  192. var
  193. s: string;
  194. begin
  195. Result := False;
  196. FSock.SendString('AUTH CRAM-MD5' + CRLF);
  197. if ReadResult <> 334 then
  198. Exit;
  199. s := Copy(FResultString, 5, Length(FResultString) - 4);
  200. s := DecodeBase64(s);
  201. s := HMAC_MD5(s, FPassword);
  202. s := FUsername + ' ' + StrToHex(s);
  203. FSock.SendString(EncodeBase64(s) + CRLF);
  204. Result := ReadResult = 235;
  205. end;
  206. function TSMTPSend.Connect: Boolean;
  207. begin
  208. FSock.CloseSocket;
  209. FSock.CreateSocket;
  210. FSock.Connect(FSMTPHost, FSMTPPort);
  211. Result := FSock.LastError = 0;
  212. end;
  213. function TSMTPSend.Helo: Boolean;
  214. var
  215. x: Integer;
  216. begin
  217. FSock.SendString('HELO ' + FSystemName + CRLF);
  218. x := ReadResult;
  219. Result := (x >= 250) and (x <= 259);
  220. end;
  221. function TSMTPSend.Ehlo: Boolean;
  222. var
  223. x: Integer;
  224. begin
  225. FSock.SendString('EHLO ' + FSystemName + CRLF);
  226. x := ReadResult;
  227. Result := (x >= 250) and (x <= 259);
  228. end;
  229. function TSMTPSend.Login: Boolean;
  230. var
  231. n: Integer;
  232. auths: string;
  233. s: string;
  234. begin
  235. Result := False;
  236. FESMTP := True;
  237. FAuthDone := False;
  238. FESMTPcap.clear;
  239. FESMTPSize := False;
  240. FMaxSize := 0;
  241. if not Connect then
  242. Exit;
  243. if ReadResult <> 220 then
  244. Exit;
  245. if not Ehlo then
  246. begin
  247. FESMTP := False;
  248. if not Helo then
  249. Exit;
  250. end;
  251. Result := True;
  252. if FESMTP then
  253. begin
  254. for n := 1 to FFullResult.Count - 1 do
  255. FESMTPcap.Add(Copy(FFullResult.Items[n], 5, Length(FFullResult.Items[n]) - 4));
  256. if not ((FUsername = '') and (FPassword = '')) then
  257. begin
  258. s := FindCap('AUTH ');
  259. if s = '' then
  260. s := FindCap('AUTH=');
  261. auths := UpperCase(s);
  262. if s <> '' then
  263. begin
  264. if Pos('CRAM-MD5', auths) > 0 then
  265. FAuthDone := AuthCram;
  266. if (Pos('LOGIN', auths) > 0) and (not FauthDone) then
  267. FAuthDone := AuthLogin;
  268. end;
  269. if FAuthDone then
  270. Ehlo;
  271. end;
  272. s := FindCap('SIZE');
  273. if s <> '' then
  274. begin
  275. FESMTPsize := True;
  276. FMaxSize := StrToIntDef(Copy(s, 6, Length(s) - 5), 0);
  277. end;
  278. end;
  279. end;
  280. procedure TSMTPSend.Logout;
  281. begin
  282. FSock.SendString('QUIT' + CRLF);
  283. ReadResult;
  284. FSock.CloseSocket;
  285. end;
  286. function TSMTPSend.Reset: Boolean;
  287. begin
  288. FSock.SendString('RSET' + CRLF);
  289. Result := ReadResult = 250;
  290. end;
  291. function TSMTPSend.NoOp: Boolean;
  292. begin
  293. FSock.SendString('NOOP' + CRLF);
  294. Result := ReadResult = 250;
  295. end;
  296. function TSMTPSend.MailFrom(const Value: string; Size: Integer): Boolean;
  297. var
  298. s: string;
  299. begin
  300. s := 'MAIL FROM:<' + Value + '>';
  301. if FESMTPsize and (Size > 0) then
  302. s := s + ' SIZE=' + Int2Str(Size);
  303. FSock.SendString(s + CRLF);
  304. Result := ReadResult = 250;
  305. end;
  306. function TSMTPSend.MailTo(const Value: string): Boolean;
  307. begin
  308. FSock.SendString('RCPT TO:<' + Value + '>' + CRLF);
  309. Result := ReadResult = 250;
  310. end;
  311. function TSMTPSend.MailData(Value: PStrList): Boolean;
  312. var
  313. n,l: Integer;
  314. s: string;
  315. begin
  316. Result := False;
  317. l := Value.Count;
  318. FSock.SendString('DATA' + CRLF);
  319. if ReadResult <> 354 then
  320. Exit;
  321. for n := 0 to l - 1 do
  322. begin
  323. s := Value.Items[n];
  324. if Length(s) >= 1 then
  325. if s[1] = '.' then
  326. s := '.' + s;
  327. FSock.SendString(s + CRLF);
  328. end;
  329. FSock.SendString('.' + CRLF);
  330. Result := ReadResult = 250;
  331. end;
  332. function TSMTPSend.Etrn(const Value: string): Boolean;
  333. var
  334. x: Integer;
  335. begin
  336. FSock.SendString('ETRN ' + Value + CRLF);
  337. x := ReadResult;
  338. Result := (x >= 250) and (x <= 259);
  339. end;
  340. function TSMTPSend.Verify(const Value: string): Boolean;
  341. var
  342. x: Integer;
  343. begin
  344. FSock.SendString('VRFY ' + Value + CRLF);
  345. x := ReadResult;
  346. Result := (x >= 250) and (x <= 259);
  347. end;
  348. function TSMTPSend.EnhCodeString: string;
  349. var
  350. s, t: string;
  351. begin
  352. s := Int2Str(FEnhCode2) + '.' + Int2Str(FEnhCode3);
  353. t := '';
  354. if s = '0.0' then t := 'Other undefined Status';
  355. if s = '1.0' then t := 'Other address status';
  356. if s = '1.1' then t := 'Bad destination mailbox address';
  357. if s = '1.2' then t := 'Bad destination system address';
  358. if s = '1.3' then t := 'Bad destination mailbox address syntax';
  359. if s = '1.4' then t := 'Destination mailbox address ambiguous';
  360. if s = '1.5' then t := 'Destination mailbox address valid';
  361. if s = '1.6' then t := 'Mailbox has moved';
  362. if s = '1.7' then t := 'Bad sender''s mailbox address syntax';
  363. if s = '1.8' then t := 'Bad sender''s system address';
  364. if s = '2.0' then t := 'Other or undefined mailbox status';
  365. if s = '2.1' then t := 'Mailbox disabled, not accepting messages';
  366. if s = '2.2' then t := 'Mailbox full';
  367. if s = '2.3' then t := 'Message Length exceeds administrative limit';
  368. if s = '2.4' then t := 'Mailing list expansion problem';
  369. if s = '3.0' then t := 'Other or undefined mail system status';
  370. if s = '3.1' then t := 'Mail system full';
  371. if s = '3.2' then t := 'System not accepting network messages';
  372. if s = '3.3' then t := 'System not capable of selected features';
  373. if s = '3.4' then t := 'Message too big for system';
  374. if s = '3.5' then t := 'System incorrectly configured';
  375. if s = '4.0' then t := 'Other or undefined network or routing status';
  376. if s = '4.1' then t := 'No answer from host';
  377. if s = '4.2' then t := 'Bad connection';
  378. if s = '4.3' then t := 'Routing server failure';
  379. if s = '4.4' then t := 'Unable to route';
  380. if s = '4.5' then t := 'Network congestion';
  381. if s = '4.6' then t := 'Routing loop detected';
  382. if s = '4.7' then t := 'Delivery time expired';
  383. if s = '5.0' then t := 'Other or undefined protocol status';
  384. if s = '5.1' then t := 'Invalid command';
  385. if s = '5.2' then t := 'Syntax error';
  386. if s = '5.3' then t := 'Too many recipients';
  387. if s = '5.4' then t := 'Invalid command arguments';
  388. if s = '5.5' then t := 'Wrong protocol version';
  389. if s = '6.0' then t := 'Other or undefined media error';
  390. if s = '6.1' then t := 'Media not supported';
  391. if s = '6.2' then t := 'Conversion required and prohibited';
  392. if s = '6.3' then t := 'Conversion required but not supported';
  393. if s = '6.4' then t := 'Conversion with loss performed';
  394. if s = '6.5' then t := 'Conversion failed';
  395. if s = '7.0' then t := 'Other or undefined security status';
  396. if s = '7.1' then t := 'Delivery not authorized, message refused';
  397. if s = '7.2' then t := 'Mailing list expansion prohibited';
  398. if s = '7.3' then t := 'Security conversion required but not possible';
  399. if s = '7.4' then t := 'Security features not supported';
  400. if s = '7.5' then t := 'Cryptographic failure';
  401. if s = '7.6' then t := 'Cryptographic algorithm not supported';
  402. if s = '7.7' then t := 'Message integrity failure';
  403. s := '???-';
  404. if FEnhCode1 = 2 then s := 'Success-';
  405. if FEnhCode1 = 4 then s := 'Persistent Transient Failure-';
  406. if FEnhCode1 = 5 then s := 'Permanent Failure-';
  407. Result := s + t;
  408. end;
  409. function TSMTPSend.FindCap(const Value: string): string;
  410. var
  411. n: Integer;
  412. s: string;
  413. begin
  414. s := UpperCase(Value);
  415. Result := '';
  416. for n := 0 to FESMTPcap.Count - 1 do
  417. if Pos(s, UpperCase(FESMTPcap.Items[n])) = 1 then
  418. begin
  419. Result := FESMTPcap.Items[n];
  420. Break;
  421. end;
  422. end;
  423. {==============================================================================}
  424. function SendToRaw(const MailFrom, MailTo, SMTPHost: string;
  425. MailData: PStrList; const Username, Password: string): Boolean;
  426. var
  427. SMTP: PSMTPSend;
  428. begin
  429. Result := False;
  430. SMTP := NewSMTPSend;
  431. try
  432. SMTP.SMTPHost := SMTPHost;
  433. SMTP.Username := Username;
  434. SMTP.Password := Password;
  435. if SMTP.Login then
  436. begin
  437. if SMTP.MailFrom(MailFrom, Length(MailData.Text)) then
  438. if SMTP.MailTo(MailTo) then
  439. if SMTP.MailData(MailData) then
  440. Result := True;
  441. SMTP.Logout;
  442. end;
  443. finally
  444. SMTP.Free;
  445. end;
  446. end;
  447. function SendToEx(const MailFrom, MailTo, Subject, SMTPHost: string;
  448. MailData: PStrList; const Username, Password: string): Boolean;
  449. var
  450. t: PStrList;
  451. begin
  452. t := NewStrList;
  453. try
  454. t.Assign(MailData);
  455. t.Insert(0, '');
  456. t.Insert(0, 'x-mailer: Synapse - Delphi TCP/IP library by Lukas Gebauer');
  457. t.Insert(0, 'subject: ' + Subject);
  458. t.Insert(0, 'date: ' + Rfc822DateTime(now));
  459. t.Insert(0, 'to: ' + MailTo);
  460. t.Insert(0, 'from: ' + MailFrom);
  461. Result := SendToRaw(MailFrom, MailTo, SMTPHost, t, Username, Password);
  462. finally
  463. t.Free;
  464. end;
  465. end;
  466. function SendTo(const MailFrom, MailTo, Subject, SMTPHost: string;
  467. MailData: PStrList): Boolean;
  468. begin
  469. Result := SendToEx(MailFrom, MailTo, Subject, SMTPHost, MailData, '', '');
  470. end;
  471. end.