Buscar

Enviando email com a biblioteca indy

Código

Unit EnviaEmail;

interface
uses SysUtils,IdMessage, IdSMTP;
type
  TEmail = class
  IdSMTP1: TIdSMTP;
  IdMessage1: TIdMessage;
  Constructor Create;
  private

  public
    function EnviarEmail(De,Para,Titulo,Corpo,Arquivo:String):boolean;
end;

implementation

Constructor TEmail.Create;
begin
  IdSMTP1 := TIdSMTP.Create(nil);
  IdMessage1 := TIdMessage.Create(nil);
end;

function TEmail.EnviarEmail(De,Para,Titulo,Corpo,Arquivo:String):boolean;
begin
  try
    IdMessage1.Clear;
    IdMessage1.From.Text :=De;
    IdMessage1.ReplyTo.EMailAddresses:=De;
    IdMessage1.Recipients.EMailAddresses:=Para;
    IdMessage1.Date:=Now;
    IdMessage1.Subject:=Titulo;
    IdMessage1.Priority:=mpNormal;
    if (Arquivo<>'') then
      TIdAttachment.Create(IdMessage1.MessageParts,Arquivo);
    IdMessage1.Body.Add(Corpo);
    IdSMTP1.AuthenticationType:=atLogin;
    IdSMTP1.Host:='smtp.dominio.com.br';
    IdSMTP1.Port:=25;
    IdSMTP1.UserName:='usuario';
    IdSMTP1.Password:='senha';
    IdSMTP1.Connect;
    result := true;
    try
      try
        IdSMTP1.Send(IdMessage1);
      except
        result := false;
      end;
    finally
      IdSMTP1.Disconnect;
    end;
  except
     result := false;
  end
end;
end.
//--------------------------------
// MAICON CARLOS PEREIRA
Exemplo:
Var
 Email : TEmail;
begin
 Email := TEmail.Create;
 Email.EnviarEmail('maiconcp@terra.com.br', 'maiconcp@terra.com.br', 'Titulo', 'MSG', '');

 

Publicidade

Vote na dica




Quantidade de votos: 0 votos
Aceitação: 0%


Detalhes da dica

Categoria: Rede
Adicionada dia: 09/05/07
Por: Maicon Carlos Pereira
Visualizada: 7157 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009