This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Nova implementação para envio de e-mail #10
Open
imozart
wants to merge
4
commits into
master
Choose a base branch
from
feature/atlas-mail
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
unit Mail.Indy; | ||
|
||
interface | ||
|
||
uses | ||
System.SysUtils, | ||
IdSMTP, | ||
IdMessage, | ||
IdSSLOpenSSL, | ||
IdExplicitTLSClientServerBase, | ||
IdText, | ||
IdAttachmentFile, | ||
MailInterface, | ||
MailBase; | ||
|
||
type | ||
EMailIndyException = class(EMailException); | ||
|
||
TMailIndy = class(TMailBase, IMail) | ||
private const | ||
CONNECT_TIMEOUT = 10000; | ||
READ_TIMEOUT = 10000; | ||
private | ||
procedure ConfigureSmtp(const ASmtp: TIdSMTP); | ||
procedure AddToRecipients(const AMsg: TIdMessage); | ||
procedure AddCcRecipients(const AMsg: TIdMessage); | ||
procedure AddBccRecipients(const AMsg: TIdMessage); | ||
procedure AddFrom(const AMsg: TIdMessage); | ||
procedure AddReplyTo(const AMsg: TIdMessage); | ||
procedure AddAttachments(const AMsg: TIdMessage); | ||
procedure AddBody(const AMsg: TIdMessage); | ||
protected | ||
procedure DoSend; override; | ||
public | ||
class function New: IMail; static; | ||
end; | ||
|
||
implementation | ||
|
||
{ TMailIndy } | ||
|
||
procedure TMailIndy.AddAttachments(const AMsg: TIdMessage); | ||
var | ||
i: Integer; | ||
attachment: TIdAttachmentFile; | ||
begin | ||
for i := 0 to Pred(GetAttachments.Count) do | ||
begin | ||
attachment := TIdAttachmentFile.Create(AMsg.MessageParts, GetAttachments[i]); | ||
attachment.Headers.Add(Format('Content-ID: <%s>', [ExtractFileName(GetAttachments[i])])); | ||
end; | ||
end; | ||
|
||
procedure TMailIndy.AddBccRecipients(const AMsg: TIdMessage); | ||
var | ||
i: Integer; | ||
begin | ||
for i := 0 to Pred(GetBccRecipients.Count) do | ||
begin | ||
with AMsg.BccList.Add do | ||
begin | ||
Address := GetBccRecipients[i]; | ||
end; | ||
end; | ||
end; | ||
|
||
procedure TMailIndy.AddBody(const AMsg: TIdMessage); | ||
var | ||
body: TIdText; | ||
begin | ||
body := TIdText.Create(AMsg.MessageParts); | ||
body.Body.Text := GetMessages.Text; | ||
body.ContentType := 'text/plain'; | ||
if IsWithHTML then | ||
begin | ||
body.ContentType := 'text/html'; | ||
end; | ||
end; | ||
|
||
procedure TMailIndy.AddCcRecipients(const AMsg: TIdMessage); | ||
var | ||
i: Integer; | ||
begin | ||
for i := 0 to Pred(GetCcRecipients.Count) do | ||
begin | ||
with AMsg.CCList.Add do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vamos usar o with mesmo? Não vale a pena evitar não? |
||
begin | ||
Address := GetCcRecipients[i]; | ||
end; | ||
end; | ||
end; | ||
|
||
procedure TMailIndy.AddFrom(const AMsg: TIdMessage); | ||
begin | ||
AMsg.From.Address := GetFromAddress; | ||
AMsg.From.Name := GetFromName; | ||
if IsWithConfirmation then | ||
begin | ||
AMsg.ReceiptRecipient.Address := GetFromAddress; | ||
AMsg.ReceiptRecipient.Name := GetFromName; | ||
end; | ||
end; | ||
|
||
procedure TMailIndy.AddReplyTo(const AMsg: TIdMessage); | ||
begin | ||
with AMsg.ReplyTo.Add do | ||
begin | ||
Address := GetReplyToAddress; | ||
Name := GetReplyToName; | ||
end; | ||
end; | ||
|
||
procedure TMailIndy.AddToRecipients(const AMsg: TIdMessage); | ||
var | ||
i: Integer; | ||
begin | ||
for i := 0 to Pred(GetToRecipients.Count) do | ||
begin | ||
with AMsg.Recipients.Add do | ||
begin | ||
Address := GetToRecipients[i]; | ||
end; | ||
end; | ||
end; | ||
|
||
procedure TMailIndy.ConfigureSmtp(const ASmtp: TIdSMTP); | ||
begin | ||
ASmtp.ConnectTimeout := CONNECT_TIMEOUT; | ||
ASmtp.ReadTimeout := READ_TIMEOUT; | ||
ASmtp.Host := GetHost; | ||
ASmtp.Username := GetUsername; | ||
ASmtp.Password := GetPassword; | ||
ASmtp.Port := GetPort; | ||
ASmtp.AuthType := satNone; | ||
if IsWithAuthentication then | ||
begin | ||
ASmtp.AuthType := satDefault; | ||
end; | ||
if IsWithSSL then | ||
begin | ||
ASmtp.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(ASmtp); | ||
TIdSSLIOHandlerSocketOpenSSL(ASmtp.IOHandler).SSLOptions.Method := sslvSSLv23; | ||
TIdSSLIOHandlerSocketOpenSSL(ASmtp.IOHandler).SSLOptions.Mode := sslmClient; | ||
ASmtp.UseTLS := utUseExplicitTLS; | ||
end; | ||
if IsWithTLS then | ||
begin | ||
ASmtp.UseTLS := utUseRequireTLS; | ||
end; | ||
end; | ||
|
||
procedure TMailIndy.DoSend; | ||
var | ||
smtp: TIdSMTP; | ||
msg: TIdMessage; | ||
begin | ||
inherited; | ||
smtp := TIdSMTP.Create(nil); | ||
try | ||
try | ||
ConfigureSmtp(smtp); | ||
msg := TIdMessage.Create(nil); | ||
try | ||
msg.Date := Now; | ||
msg.Subject := GetSubject; | ||
msg.ContentType := 'multipart/mixed'; | ||
|
||
AddToRecipients(msg); | ||
AddCcRecipients(msg); | ||
AddBccRecipients(msg); | ||
AddFrom(msg); | ||
AddReplyTo(msg); | ||
AddAttachments(msg); | ||
AddBody(msg); | ||
|
||
smtp.Connect; | ||
try | ||
if IsWithAuthentication then | ||
begin | ||
smtp.Authenticate; | ||
end; | ||
smtp.Send(msg); | ||
finally | ||
smtp.Disconnect; | ||
end; | ||
Comment on lines
+177
to
+185
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acredito que aqui a gente pode mesclar os dois Try..Finally, e eliminar esse interno, levando o |
||
finally | ||
FreeAndNil(msg); | ||
end; | ||
except | ||
on E: Exception do | ||
begin | ||
raise EMailIndyException.Create('E-mail could not be sent!' + ^M + E.Message); | ||
end; | ||
end; | ||
finally | ||
FreeAndNil(smtp); | ||
end; | ||
end; | ||
|
||
class function TMailIndy.New: IMail; | ||
begin | ||
Result := TMailIndy.Create; | ||
end; | ||
|
||
end. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Talvez trocar a variável por HaveHtmlContent, ou IsHtmlFormat. Ficou meio vago esse nome.