Quick.SMTP.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. { ***************************************************************************
  2. Copyright (c) 2016-2018 Kike Pérez
  3. Unit : Quick.SMTP
  4. Description : Send Emails
  5. Author : Kike Pérez
  6. Version : 1.2
  7. Created : 12/10/2017
  8. Modified : 07/04/2018
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.SMTP;
  22. interface
  23. {$i QuickLib.inc}
  24. uses
  25. Classes,
  26. SysUtils,
  27. IdGlobal,
  28. IdSMTP,
  29. IdMessage,
  30. IdReplySMTP,
  31. IdSSLOpenSSL,
  32. IdText,
  33. IdAttachment,
  34. IdAttachmentFile,
  35. IdExplicitTLSClientServerBase,
  36. IdHTTP;
  37. type
  38. TMailMessage = class
  39. private
  40. fSenderName : string;
  41. fFrom : string;
  42. fRecipient : string;
  43. fSubject : string;
  44. fBody : string;
  45. fCC : string;
  46. fBCC : string;
  47. fBodyFromFile : Boolean;
  48. fAttachments : TStringList;
  49. procedure SetBody(aValue : string);
  50. public
  51. constructor Create;
  52. destructor Destroy; override;
  53. property SenderName : string read fSenderName write fSenderName;
  54. property From : string read fFrom write fFrom;
  55. property Recipient : string read fRecipient write fRecipient;
  56. property Subject : string read fSubject write fSubject;
  57. property Body : string read fBody write SetBody;
  58. property CC : string read fCC write fCC;
  59. property BCC : string read fBCC write fBCC;
  60. property Attachments : TStringList read fAttachments write fAttachments;
  61. procedure AddBodyFromFile(const cFileName : string);
  62. end;
  63. TSMTP = class(TIdSMTP)
  64. private
  65. fServerAuth : Boolean;
  66. fUseSSL : Boolean;
  67. fMail : TMailMessage;
  68. public
  69. constructor Create; overload;
  70. constructor Create(const cHost : string; cPort : Integer; cUseSSL : Boolean = False); overload;
  71. destructor Destroy; override;
  72. property ServerAuth : Boolean read fServerAuth write fServerAuth;
  73. property UseSSL: Boolean read fUseSSL write fUseSSL;
  74. property Mail : TMailMessage read fMail write fMail;
  75. function SendMail: Boolean; overload;
  76. function SendEmail(const cFrom,cSubject,cTo,cBody : string) : Boolean; overload;
  77. end;
  78. implementation
  79. { TMailMessage }
  80. constructor TMailMessage.Create;
  81. begin
  82. fCC := '';
  83. fBCC := '';
  84. fBody := '';
  85. fAttachments := TStringList.Create;
  86. end;
  87. destructor TMailMessage.Destroy;
  88. begin
  89. if Assigned(fAttachments) then fAttachments.Free;
  90. inherited;
  91. end;
  92. procedure TMailMessage.AddBodyFromFile(const cFileName: string);
  93. begin
  94. fBodyFromFile := True;
  95. fBody := cFileName;
  96. end;
  97. procedure TMailMessage.SetBody(aValue: string);
  98. begin
  99. fBodyFromFile := False;
  100. fBody := aValue;
  101. end;
  102. { TSMTP }
  103. constructor TSMTP.Create;
  104. begin
  105. inherited Create;
  106. fMail := TMailMessage.Create;
  107. Port := 25;
  108. UseTLS := TIdUseTLS.utNoTLSSupport;
  109. fUseSSL := False;
  110. end;
  111. constructor TSMTP.Create(const cHost : string; cPort : Integer; cUseSSL : Boolean = False);
  112. begin
  113. inherited Create;
  114. Host := cHost;
  115. Port := cPort;
  116. fUseSSL := cUseSSL;
  117. end;
  118. destructor TSMTP.Destroy;
  119. begin
  120. if Assigned(fMail) then fMail.Free;
  121. inherited;
  122. end;
  123. function TSMTP.SendEmail(const cFrom,cSubject,cTo,cBody : string) : Boolean;
  124. begin
  125. fMail.From := cFrom;
  126. fMail.Subject := cSubject;
  127. fMail.Body := cBody;
  128. fMail.Recipient := cTo;
  129. fMail.CC := '';
  130. fMail.BCC := '';
  131. Result := Self.SendMail;
  132. end;
  133. function TSMTP.SendMail: Boolean;
  134. var
  135. msg : TIdMessage;
  136. SSLHandler : TIdSSLIOHandlerSocketOpenSSL;
  137. email : string;
  138. filename : string;
  139. mBody : TIdText;
  140. idattach : TIdAttachmentFile;
  141. begin
  142. Result := False;
  143. SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  144. try
  145. msg := TIdMessage.Create(nil);
  146. try
  147. //create mail msg
  148. idattach := nil;
  149. mBody := nil;
  150. msg.From.Address := fMail.From;
  151. if fMail.SenderName <> '' then msg.From.Name := fMail.SenderName;
  152. msg.Subject := fMail.Subject;
  153. for email in fMail.Recipient.Split([',']) do msg.Recipients.Add.Address := email;
  154. for email in fMail.CC.Split([',']) do msg.CCList.Add.Address := email;
  155. for email in fMail.BCC.Split([',']) do msg.BCCList.Add.Address := email;
  156. if fMail.fBodyFromFile then
  157. begin
  158. msg.Body.LoadFromFile(fMail.Body);
  159. end
  160. else
  161. begin
  162. mBody := TIdText.Create(msg.MessageParts);
  163. mBody.ContentType := 'text/html';
  164. mBody.Body.Text := fMail.Body;
  165. end;
  166. //add attachements if exists
  167. if fMail.Attachments.Count > 0 then
  168. begin
  169. for filename in fMail.Attachments do
  170. begin
  171. idattach := TIdAttachmentFile.Create(msg.MessageParts,filename);
  172. end;
  173. end;
  174. //configure smtp SSL
  175. try
  176. if fUseSSL then
  177. begin
  178. Self.IOHandler := SSLHandler;
  179. SSLHandler.MaxLineAction := maException;
  180. SSLHandler.SSLOptions.Method := sslvTLSv1_2;
  181. SSLHandler.SSLOptions.Mode := sslmUnassigned;
  182. SSLHandler.SSLOptions.VerifyMode := [];
  183. SSLHandler.SSLOptions.VerifyDepth := 0;
  184. //Self.UseTLS := utUseExplicitTLS;
  185. end;
  186. //server auth
  187. if ServerAuth then Self.AuthType := TIdSMTPAuthenticationType.satDefault;
  188. Self.Port := fPort;
  189. except
  190. on E : Exception do raise Exception.Create(Format('[%s] : %s',[Self.ClassName,e.Message]));
  191. end;
  192. //send email
  193. try
  194. Self.Connect;
  195. if Self.Connected then
  196. begin
  197. Self.Send(msg);
  198. Self.Disconnect(False);
  199. Result := True;
  200. end;
  201. except
  202. on E : Exception do raise Exception.Create(Format('[%s] : %s',[Self.ClassName,e.Message]));
  203. end;
  204. finally
  205. if Assigned(idattach) then idattach.Free;
  206. if Assigned(mBody) then mBody.Free;
  207. msg.Free;
  208. end;
  209. finally
  210. SSLHandler.Free;
  211. end;
  212. end;
  213. end.