Fórum Componente para envio de mensagens de erro por e-mail #357160
14/04/2008
0
Desenvolvi um componente para enviar mensagens de erro por email e gostaria da opinião de vocês e também da ajuda para melhorá-lo:
unit SendException;
interface
uses
//---------------------------------------------------------------
//indy
IdTCPClient,IdMessageClient, IdSMTP,IdMessage,IdAntiFreezeBase,
IdAntiFreeze,IdBaseComponent, IdComponent, IdTCPConnection,
//---------------------------------------------------------------
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ClipBrd, Jpeg;
type
TSendException = class(TComponent)
private
{ Private declarations }
//-----------------------------------------------------------------------------
//Propriedades
FEmailFrom:string;
FEmailTo:string;
FHost: String;
FUsername:String;
FPassword:String;
FPort:Integer;
FFromName:string;
//-----------------------------------------------------------------------------
//Componentes internos
IdSMTP: TIdSMTP;
IdMessage: TIdMessage;
IdAntiFreeze:TIdAntiFreeze;
ArquivoImagem:string;
TCFLib_MsgDlg:TEvMsgDlg;
//-----------------------------------------------------------------------------
procedure CapturaTela;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create ( AOwner : TComponent ); override;
destructor Destroy; override;
procedure SendException(E:Exception);
published
{ Published declarations }
property EmailFrom :String read FEmailfrom write FEmailfrom;
property EmailTo :String read FEmailTo write FEmailTo;
property Host :String read FHost write FHost;
property Username :String read FUsername write FUsername;
property Password :String read FPassword write FPassword;
property Port :Integer read FPort write FPort;
property FromName :String read FFromName write FFromName;
end;
procedure Register;
implementation
uses dm_SendException;
procedure Register;
begin
RegisterComponents(´Samples´, [TSendException]);
end;
{ TSendException }
{ TSendException }
procedure TSendException.CapturaTela;
var
bmp : TBitmap;
jpeg : TJPEGImage;
dia,mes,ano,hora,min,seg, mseg : word;
begin
bmp := TBitmap.Create;
Clipboard.Clear;
keybd_event(vk_snapshot,1, 0, 0);
Application.ProcessMessages;
bmp.LoadFromClipboardFormat(CF_BITMAP,clipboard.GetAsHandle(CF_BITMAP),0);
jpeg := TJPEGImage.Create;
jpeg.Assign(bmp);
DecodeDate(now,ano,mes,dia);
DecodeTime(now,hora,min,seg,mseg);
ArquivoImagem := ExtractFilePath(Application.ExeName) + inttostr(dia) + ´.´ + inttostr(mes) + ´.´ + inttostr(ano) + ´.´ + inttostr(hora) + ´.´ + inttostr(min) + ´.´ + inttostr(seg) + ´.jpg´;
jpeg.SaveToFile(ArquivoImagem);
jpeg.Free;
bmp.Free;
end;
constructor TSendException.Create(AOwner: TComponent);
begin
inherited;
IdSMTP := TIdSMTP.Create(Self);
IdMessage := TIdMessage.Create(self);
IdAntiFreeze := TIdAntiFreeze.Create(self);
end;
destructor TSendException.Destroy;
begin
FreeAndNil(IdSMTP);
FreeAndNil(IdMessage);
FreeAndNil(IdAntiFreeze);
FreeAndNil(TCFLib_MsgDlg);
inherited;
end;
procedure TSendException.SendException(E: Exception);
begin
try
CapturaTela;
IdMessage.Subject :=´Error: ´+DateTimeToStr(Now);
IdMessage.Body.Add(´A aplicação ´+Application.Title+´ gerou o seguinte erro:´);
IdMessage.Body.Add(´´);
IdMessage.Body.Add(e.Message+´.´);
IdMessage.From.Address := FEmailFrom;
IdMessage.From.Name := FFromName;
IdMessage.Recipients.EMailAddresses := FEmailTo;
IdMessage.ContentType := ´text/html´;
IdMessage.MessageParts.Clear;
TIdAttachment.Create(IdMessage.MessageParts, ArquivoImagem);
showmessage(IdMessage.Body.Text+#13+
´Um e-mail foi enviado ao nosso departamento de suporte para ´+13+
´análise e entraremos em contato o mais breve possível.´+13+
´Obrigado!´);
IdSMTP.Host := FHost;
IdSMTP.Username := FUsername;
IdSMTP.Password := FPassword;
IdSMTP.Port := FPort;
IdSMTP.AuthenticationType := atLogin;
try
if not IdSMTP.Connected then
IdSMTP.Connect();
IdSMTP.Send(IdMessage);
except
on e: Exception do
begin
TCFLib_MsgDlg.MsgError(´Não foi possível o envio da mensagem de erro.´+#13+
´Mensagem gerada:´+13+
e.Message+13+
´Obrigado.´);
abort;
end;
end;
finally
IdSMTP.Disconnect;
DeleteFile(ArquivoImagem);
end;
end;
end.
Janderson
Curtir tópico
+ 0Posts
15/04/2008
Walter Faria
estava prescisando de um componente deste, vc poderia me passar
walter@rwconsultoria.com.br
Gostei + 0
16/04/2008
Facc
Vc poderia disponibilizar o componente em si... assim fica mais facil para nós testarmos.
Gostei + 0
16/04/2008
Janderson
Gostei + 0
16/04/2008
Facc
faccruz@gmail.com
Funciona em BDS2006?
Gostei + 0
16/04/2008
Janderson
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)