5
5
Unit : Quick.SMTP
6
6
Description : Send Emails
7
7
Author : Kike P�rez
8
- Version : 1.4
8
+ Version : 1.5
9
9
Created : 12/10/2017
10
- Modified : 11/05 /2021
10
+ Modified : 08/09 /2021
11
11
12
12
This file is part of QuickLib: https://github.com/exilon/QuickLib
13
13
@@ -35,6 +35,7 @@ interface
35
35
36
36
uses
37
37
Classes,
38
+ Generics.Collections,
38
39
SysUtils,
39
40
IdGlobal,
40
41
IdSMTP,
@@ -44,10 +45,21 @@ interface
44
45
IdText,
45
46
IdAttachment,
46
47
IdAttachmentFile,
48
+ IdAttachmentMemory,
47
49
IdExplicitTLSClientServerBase,
48
50
IdHTTP;
49
51
50
52
type
53
+ TAttachment = class
54
+ private
55
+ fFilename : string;
56
+ fContent : TStream;
57
+ public
58
+ constructor Create(const aFilename : string; const aContent : TStream);
59
+ destructor Destroy; override;
60
+ property Filename : string read fFilename;
61
+ property Content : TStream read fContent;
62
+ end ;
51
63
52
64
TMailMessage = class
53
65
private
@@ -61,7 +73,9 @@ TMailMessage = class
61
73
fReplyTo : string;
62
74
fBodyFromFile : Boolean;
63
75
fAttachments : TStringList;
76
+ fAttachmentFiles : TObjectList<TAttachment>;
64
77
procedure SetBody (aValue : string);
78
+ procedure SetAttachments (const Value : TStringList);
65
79
public
66
80
constructor Create;
67
81
destructor Destroy; override;
@@ -73,7 +87,10 @@ TMailMessage = class
73
87
property CC : string read fCC write fCC;
74
88
property BCC : string read fBCC write fBCC;
75
89
property ReplyTo : string read fReplyTo write fReplyTo;
76
- property Attachments : TStringList read fAttachments write fAttachments;
90
+ property Attachments : TStringList read fAttachments write SetAttachments;
91
+ property AttachmentFiles : TObjectList<TAttachment> read fAttachmentFiles;
92
+ procedure AddAttachment (const aFilename : string; aStream : TStream); overload;
93
+ procedure AddAttachment (const aFilename, aFilePath : string); overload;
77
94
procedure AddBodyFromFile (const cFileName : string);
78
95
end ;
79
96
@@ -107,20 +124,43 @@ constructor TMailMessage.Create;
107
124
fBCC := ' ' ;
108
125
fBody := ' ' ;
109
126
fAttachments := TStringList.Create;
127
+ fAttachmentFiles := TObjectList<TAttachment>.Create(True);
110
128
end ;
111
129
112
130
destructor TMailMessage.Destroy;
113
131
begin
114
132
if Assigned(fAttachments) then fAttachments.Free;
133
+ if Assigned(fAttachmentFiles) then fAttachmentFiles.Free;
115
134
inherited ;
116
135
end ;
117
136
137
+ procedure TMailMessage.AddAttachment (const aFilename, aFilePath : string);
138
+ var
139
+ fs : TFileStream;
140
+ begin
141
+ if not FileExists(aFilePath) then raise Exception.CreateFmt(' MailMessage: file "%s" not found!' ,[aFilename]);
142
+ fs := TFileStream.Create(aFilePath,fmOpenRead);
143
+ fAttachmentFiles.Add(TAttachment.Create(aFilename,fs));
144
+ end ;
145
+
146
+ procedure TMailMessage.AddAttachment (const aFilename : string; aStream : TStream);
147
+ begin
148
+ fAttachmentFiles.Add(TAttachment.Create(aFilename,aStream));
149
+ end ;
150
+
118
151
procedure TMailMessage.AddBodyFromFile (const cFileName: string);
119
152
begin
120
153
fBodyFromFile := True;
121
154
fBody := cFileName;
122
155
end ;
123
156
157
+ procedure TMailMessage.SetAttachments (const Value : TStringList);
158
+ begin
159
+ if Assigned(fAttachments) then fAttachments.Free;
160
+
161
+ fAttachments := Value ;
162
+ end ;
163
+
124
164
procedure TMailMessage.SetBody (aValue: string);
125
165
begin
126
166
fBodyFromFile := False;
@@ -198,7 +238,8 @@ function TSMTP.SendMail(aMail : TMailMessage) : Boolean;
198
238
email : string;
199
239
filename : string;
200
240
mBody : TIdText;
201
- idattach : TIdAttachmentFile;
241
+ idattach : TIdAttachment;
242
+ attach : TAttachment;
202
243
begin
203
244
Result := False;
204
245
SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil );
@@ -234,6 +275,17 @@ function TSMTP.SendMail(aMail : TMailMessage) : Boolean;
234
275
idattach := TIdAttachmentFile.Create(msg.MessageParts,filename);
235
276
end ;
236
277
end ;
278
+ // add stream attachments if exists
279
+ if aMail.AttachmentFiles.Count > 0 then
280
+ begin
281
+ // mBody.ContentType := 'multipart/mixed';
282
+ // msg.ContentType := 'multipart/mixed';
283
+ for attach in aMail.AttachmentFiles do
284
+ begin
285
+ idattach := TIdAttachmentMemory.Create(msg.MessageParts,attach.Content);
286
+ idattach.Filename := attach.Filename;
287
+ end ;
288
+ end ;
237
289
238
290
// configure smtp SSL
239
291
try
@@ -278,4 +330,18 @@ function TSMTP.SendMail(aMail : TMailMessage) : Boolean;
278
330
end ;
279
331
280
332
333
+ { TAttachment }
334
+
335
+ constructor TAttachment.Create(const aFilename: string; const aContent: TStream);
336
+ begin
337
+ fFilename := aFilename;
338
+ fContent := aContent;
339
+ end ;
340
+
341
+ destructor TAttachment.Destroy;
342
+ begin
343
+ if Assigned(fContent) then fContent.Free;
344
+ inherited ;
345
+ end ;
346
+
281
347
end .
0 commit comments