-
Notifications
You must be signed in to change notification settings - Fork 7
/
UntPrincipal.pas
183 lines (159 loc) · 5.23 KB
/
UntPrincipal.pas
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
unit UntPrincipal;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls,
//Units Necessárias
IniFiles,
IdComponent,
IdTCPConnection,
IdTCPClient,
IdHTTP,
IdBaseComponent,
IdMessage,
IdExplicitTLSClientServerBase,
IdMessageClient,
IdSMTPBase,
IdSMTP,
IdIOHandler,
IdIOHandlerSocket,
IdIOHandlerStack,
IdSSL,
IdSSLOpenSSL,
IdAttachmentFile,
IdText;
type
TfrmPrincipal = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
edtAssunto: TEdit;
memCorpo: TMemo;
Label3: TLabel;
edtPara: TEdit;
edtAnexo: TEdit;
Button2: TButton;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
function EnviarEmail(const AAssunto, ADestino, AAnexo: String; ACorpo: TStrings): Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
frmPrincipal: TfrmPrincipal;
implementation
{$R *.dfm}
procedure TfrmPrincipal.Button1Click(Sender: TObject);
begin
if EnviarEmail(edtAssunto.Text, edtPara.Text, edtAnexo.Text, memCorpo.Lines)
then ShowMessage('Enviado com sucesso!')
else ShowMessage('Não foi possível enviar o e-mail!');
end;
procedure TfrmPrincipal.Button2Click(Sender: TObject);
begin
with TOpenDialog.Create(Self) do
if Execute then
edtAnexo.Text := FileName;
end;
function TfrmPrincipal.EnviarEmail(const AAssunto, ADestino, AAnexo: String; ACorpo: TStrings): Boolean;
var
IniFile : TIniFile;
sFrom : String;
sBccList : String;
sHost : String;
iPort : Integer;
sUserName : String;
sPassword : String;
idMsg : TIdMessage;
idText : TIdText;
idSMTP : TIdSMTP;
idSSLIOHandlerSocket : TIdSSLIOHandlerSocketOpenSSL;
begin
try
try
//Criação e leitura do arquivo INI com as configurações
IniFile := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'Config.ini');
sFrom := IniFile.ReadString('Email' , 'From' , sFrom);
sBccList := IniFile.ReadString('Email' , 'BccList' , sBccList);
sHost := IniFile.ReadString('Email' , 'Host' , sHost);
iPort := IniFile.ReadInteger('Email', 'Port' , iPort);
sUserName := IniFile.ReadString('Email' , 'UserName' , sUserName);
sPassword := IniFile.ReadString('Email' , 'Password' , sPassword);
//Configura os parâmetros necessários para SSL
IdSSLIOHandlerSocket := TIdSSLIOHandlerSocketOpenSSL.Create(Self);
IdSSLIOHandlerSocket.SSLOptions.Method := sslvSSLv23;
IdSSLIOHandlerSocket.SSLOptions.Mode := sslmClient;
//Variável referente a mensagem
idMsg := TIdMessage.Create(Self);
idMsg.CharSet := 'utf-8';
idMsg.Encoding := meMIME;
idMsg.From.Name := 'TESTE';
idMsg.From.Address := sFrom;
idMsg.Priority := mpNormal;
idMsg.Subject := AAssunto;
//Add Destinatário(s)
idMsg.Recipients.Add;
idMsg.Recipients.EMailAddresses := ADestino;
idMsg.CCList.EMailAddresses := 'teste@teste.com.br';
idMsg.BccList.EMailAddresses := sBccList;
idMsg.BccList.EMailAddresses := 'teste@teste.com.br'; //Cópia Oculta
//Variável do texto
idText := TIdText.Create(idMsg.MessageParts);
idText.Body.Add(ACorpo.Text);
idText.ContentType := 'text/html; text/plain; charset=iso-8859-1';
//Prepara o Servidor
idSMTP := TIdSMTP.Create(Self);
idSMTP.IOHandler := IdSSLIOHandlerSocket;
idSMTP.UseTLS := utUseImplicitTLS;
idSMTP.AuthType := satDefault;
idSMTP.Host := sHost;
idSMTP.AuthType := satDefault;
idSMTP.Port := iPort;
idSMTP.Username := sUserName;
idSMTP.Password := sPassword;
//Conecta e Autentica
idSMTP.Connect;
idSMTP.Authenticate;
if AAnexo <> EmptyStr then
if FileExists(AAnexo) then
TIdAttachmentFile.Create(idMsg.MessageParts, AAnexo);
//Se a conexão foi bem sucedida, envia a mensagem
if idSMTP.Connected then
begin
try
IdSMTP.Send(idMsg);
except on E:Exception do
begin
ShowMessage('Erro ao tentar enviar: ' + E.Message);
end;
end;
end;
//Depois de tudo pronto, desconecta do servidor SMTP
if idSMTP.Connected then
idSMTP.Disconnect;
Result := True;
finally
IniFile.Free;
UnLoadOpenSSLLibrary;
FreeAndNil(idMsg);
FreeAndNil(idSSLIOHandlerSocket);
FreeAndNil(idSMTP);
end;
except on e:Exception do
begin
Result := False;
end;
end;
end;
end.