Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Mail4Delphi.Intf.pas
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface
function Clear: IMail;
function SendMail: Boolean;
function SetUpEmail: Boolean;
function Connect: Boolean;
function Disconnect: Boolean;
end;

implementation
Expand Down
88 changes: 51 additions & 37 deletions src/Mail4Delphi.pas
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,9 @@ TMail = class(TInterfacedObject, IMail)
FSSL: Boolean;
FAuth: Boolean;
FReceiptRecipient: Boolean;
function SetUpEmail: Boolean;
protected
property IdSSLIOHandlerSocket: TIdSSLIOHandlerSocketOpenSSL read FIdSSLIOHandlerSocket write FIdSSLIOHandlerSocket;
property IdSMTP: TIdSMTP read FIdSMTP write FIdSMTP;
property IdMessage: TIdMessage read FIdMessage write FIdMessage;
property IdText: TIdText read FIdText write FIdText;
property SetSSL: Boolean read FSSL write FSSL;
property SetAuth: Boolean read FAuth write FAuth;
property SetReceiptRecipient: Boolean read FReceiptRecipient write FReceiptRecipient;
public
function AddTo(const AMail: string; const AName: string = ''): IMail;
function AddFrom(const AMail: string; const AName: string = ''): IMail;
function ReceiptRecipient(const AValue: Boolean): IMail;
function AddSubject(const ASubject: string): IMail;
function AddReplyTo(const AMail: string; const AName: string = ''): IMail;
function AddCC(const AMail: string; const AName: string = ''): IMail;
Expand All @@ -48,13 +39,24 @@ TMail = class(TInterfacedObject, IMail)
function UserName(const AUserName: string): IMail;
function Password(const APassword: string): IMail;
function Port(const APort: Integer): IMail;
function ReceiptRecipient(const AValue: Boolean): IMail;
function AddAttachment(const AFile: string): IMail;
function Auth(const AValue: Boolean): IMail;
function SSL(const AValue: Boolean): IMail;
function ContentType(const AValue: string): IMail;
function Clear: IMail;
function SendMail: Boolean;
function SetUpEmail: Boolean;
function Connect: Boolean;
function Disconnect: Boolean;
protected
property IdSSLIOHandlerSocket: TIdSSLIOHandlerSocketOpenSSL read FIdSSLIOHandlerSocket write FIdSSLIOHandlerSocket;
property IdSMTP: TIdSMTP read FIdSMTP write FIdSMTP;
property IdMessage: TIdMessage read FIdMessage write FIdMessage;
property IdText: TIdText read FIdText write FIdText;
property SetSSL: Boolean read FSSL write FSSL;
property SetAuth: Boolean read FAuth write FAuth;
property SetReceiptRecipient: Boolean read FReceiptRecipient write FReceiptRecipient;
public
class function New: IMail;
constructor Create;
destructor Destroy; override;
Expand Down Expand Up @@ -179,6 +181,34 @@ function TMail.ClearBody: IMail;
Result := Self;
end;

function TMail.Connect: Boolean;
begin
if not SetUpEmail then
raise Exception.Create('Dados incompletos!');
FIdSSLIOHandlerSocket.SSLOptions.Method := sslvTLSv1_2;
FIdSSLIOHandlerSocket.SSLOptions.Mode := sslmUnassigned;
FIdSMTP.IOHandler := IdSSLIOHandlerSocket;
FIdSMTP.UseTLS := utUseExplicitTLS;
if FSSL then
begin
FIdSSLIOHandlerSocket.SSLOptions.Mode := sslmClient;
FIdSMTP.UseTLS := utUseImplicitTLS;
end;
FIdSMTP.AuthType := satNone;
if FAuth then
FIdSMTP.AuthType := satDefault;
if FReceiptRecipient then
FIdMessage.ReceiptRecipient.Text := FIdMessage.From.Name + ' ' + FIdMessage.From.Address;
try
FIdSMTP.Connect;
FIdSMTP.Authenticate;
Result := True;
except
on E: Exception do
raise Exception.Create('Erro na conexão ou autenticação: ' + E.Message);
end;
end;

function TMail.ContentType(const AValue: string): IMail;
begin
FIdText.ContentType := AValue;
Expand Down Expand Up @@ -211,47 +241,31 @@ destructor TMail.Destroy;
FreeAndNil(FIdSMTP);
end;

function TMail.Disconnect: Boolean;
begin
FIdSMTP.Disconnect;
UnLoadOpenSSLLibrary;
Result := True;
end;

class function TMail.New: IMail;
begin
Result := TMail.Create;
end;

function TMail.SendMail: Boolean;
begin
if not SetUpEmail then
raise Exception.Create('Dados incompletos!');
Self.Connect;
try
FIdSSLIOHandlerSocket.SSLOptions.Method := sslvTLSv1_2;
FIdSSLIOHandlerSocket.SSLOptions.Mode := sslmUnassigned;
FIdSMTP.IOHandler := IdSSLIOHandlerSocket;
FIdSMTP.UseTLS := utUseExplicitTLS;
if FSSL then
begin
FIdSSLIOHandlerSocket.SSLOptions.Mode := sslmClient;
FIdSMTP.UseTLS := utUseImplicitTLS;
end;
FIdSMTP.AuthType := satNone;
if FAuth then
FIdSMTP.AuthType := satDefault;
if FReceiptRecipient then
FIdMessage.ReceiptRecipient.Text := FIdMessage.From.Name + ' ' + FIdMessage.From.Address;
try
FIdSMTP.Connect;
FIdSMTP.Authenticate;
except
on E: Exception do
raise Exception.Create('Erro na conexão ou autenticação: ' + E.Message);
end;
try
FIdSMTP.Send(IdMessage);
Result := True;
except
on E: Exception do
raise Exception.Create('Erro ao enviar a mensagem: ' + E.Message);
end;
finally
FIdSMTP.Disconnect;
UnLoadOpenSSLLibrary;
Result := True;
Self.Disconnect;
end;
end;

Expand Down