-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEmailSender.pas
More file actions
146 lines (127 loc) · 4.04 KB
/
Copy pathEmailSender.pas
File metadata and controls
146 lines (127 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
unit EmailSender;
interface
uses
System.SysUtils, System.Classes, IdSMTP, IdSSLOpenSSL, IdMessage, IdText,
IdExplicitTLSClientServerBase, IdAttachmentFile;
type
TEmailSender = class
private
FFromAddress: string;
FToAddress: string;
FSubject: string;
FBody: string;
FSMTPHost: string;
FSMTPPort: Integer;
FSMTPUsername: string;
FSMTPPassword: string;
FAttachmentFilePath: string;
procedure SetFromAddress(const Value: string);
procedure SetToAddress(const Value: string);
procedure SetSubject(const Value: string);
procedure SetBody(const Value: string);
procedure SetSMTPHost(const Value: string);
procedure SetSMTPPort(const Value: Integer);
procedure SetSMTPUsername(const Value: string);
procedure SetSMTPPassword(const Value: string);
public
procedure SendEmail;
property FromAddress: string read FFromAddress write SetFromAddress;
property ToAddress: string read FToAddress write SetToAddress;
property Subject: string read FSubject write SetSubject;
property Body: string read FBody write SetBody;
property SMTPHost: string read FSMTPHost write SetSMTPHost;
property SMTPPort: Integer read FSMTPPort write SetSMTPPort;
property SMTPUsername: string read FSMTPUsername write SetSMTPUsername;
property SMTPPassword: string read FSMTPPassword write SetSMTPPassword;
property AttachmentFilePath: string read FAttachmentFilePath
write FAttachmentFilePath;
end;
implementation
{ TEmailSender }
procedure TEmailSender.SendEmail;
var
IdSMTP: TIdSMTP;
IdSSL: TIdSSLIOHandlerSocketOpenSSL;
IdMessage: TIdMessage;
IdText: TIdText;
Attachment: TIdAttachmentFile;
Template: TStringList;
begin
IdSMTP := TIdSMTP.Create(nil);
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
IdMessage := TIdMessage.Create(nil);
Template := TStringList.Create;
try
IdSSL.SSLOptions.Method := sslvTLSv1_2;
IdSMTP.IOHandler := IdSSL;
IdSMTP.UseTLS := utUseExplicitTLS;
IdSMTP.Host := FSMTPHost;
IdSMTP.Port := FSMTPPort;
IdSMTP.AuthType := satDefault;
IdSMTP.Username := FSMTPUsername;
IdSMTP.Password := FSMTPPassword;
IdMessage.From.Address := FFromAddress;
IdMessage.Recipients.Add.Address := FToAddress;
IdMessage.Subject := FSubject;
IdMessage.MessageParts.Clear;
IdMessage.ContentType := 'multipart/mixed';
IdText := TIdText.Create(IdMessage.MessageParts, nil);
IdText.ContentType := 'text/html';
Template.LoadFromFile('Template.html');
Template.Text := StringReplace(Template.Text, '<%TituloEmail%>', FSubject,
[rfReplaceAll]);
Template.Text := StringReplace(Template.Text, '<%ConteudoEmail%>', FBody,
[rfReplaceAll]);
Template.Text := StringReplace(Template.Text, '<%RodapeEmail%>',
'Este é um email enviado pela Amil Contabilidade LTDA.', [rfReplaceAll]);
IdText.CharSet := 'ISO-8859-1';
IdText.Body.Text := Template.Text;
if FAttachmentFilePath <> '' then
begin
Attachment := TIdAttachmentFile.Create(IdMessage.MessageParts,
FAttachmentFilePath);
Attachment.ContentType := 'application/pdf;';
Attachment.FileName := ExtractFileName(FAttachmentFilePath);
end;
IdSMTP.Connect;
IdSMTP.Send(IdMessage);
finally
IdSMTP.Disconnect;
IdSMTP.Free;
IdSSL.Free;
IdMessage.Free;
end;
end;
procedure TEmailSender.SetBody(const Value: string);
begin
FBody := Value;
end;
procedure TEmailSender.SetFromAddress(const Value: string);
begin
FFromAddress := Value;
end;
procedure TEmailSender.SetSMTPHost(const Value: string);
begin
FSMTPHost := Value;
end;
procedure TEmailSender.SetSMTPPassword(const Value: string);
begin
FSMTPPassword := Value;
end;
procedure TEmailSender.SetSMTPPort(const Value: Integer);
begin
FSMTPPort := Value;
end;
procedure TEmailSender.SetSMTPUsername(const Value: string);
begin
FSMTPUsername := Value;
end;
procedure TEmailSender.SetSubject(const Value: string);
begin
FSubject := Value;
end;
procedure TEmailSender.SetToAddress(const Value: string);
begin
FToAddress := Value;
end;
end.