Utilizamos cookies propias y de terceros. [Más información sobre las cookies].
Política de cookies
Proyecto AjpdSoft

· Inicio
· Buscar
· Contactar
· Cookies
· Descargas
· Foros
· Historia
· Nosotros
· Temas
· Top 10
· Trucos
· Tutoriales
· Wiki
Formulario para envío de datos por email - Delphi
Lenguaje de programación Borland Delphi


Ejemplo formulario de envío de datos vía email. Para ello necesitaremos un Formulario (TForm), dos TEdit, un TMemo y dos TButton. A continuación mostramos todo el código fuente completo:

unit UnidadEnvioComentarios;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdHTTP, IdSMTP, IdMessage;

type
  TformEnvioComentarios = class(TForm)
    Label1: TLabel;
    txtNombre: TEdit;
    Label2: TLabel;
    txtEmail: TEdit;
    txtComentario: TMemo;
    bEnviar: TButton;
    bCerrar: TButton;
    Label3: TLabel;
    Label4: TLabel;
    procedure bCerrarClick(Sender: TObject);
    procedure bEnviarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  formEnvioComentarios: TformEnvioComentarios;

implementation

var
  compEnvioEmail : TIdSMTP;

{$R *.dfm}


//Función para conectar con el servidor de email
function ConectarServidorEmail (servidor : string; usuario : string; puerto : integer) : boolean;
begin
  compEnvioEmail := TIdSMTP.Create(nil);
  compEnvioEmail.host := servidor;
  compEnvioEmail.userid := usuario;
  compEnvioEmail.Port := puerto;
  try
    compEnvioEmail.connect;
    ConectarServidorEmail := true;
  except
    compEnvioEmail.Free;
    ConectarServidorEmail := false;
  end;
end;

//enviar email
function enviarEmail (servidor : string; usuario : string; contrasena : string;
    puerto : integer; asunto : string; mensaje : TStringList; conAutenticacion : boolean;
    emisor : string; nombreEmisor : string; destinatario : string; cc : string) : boolean;
var
  compMensaje : TIdMessage;
  envioCorrecto : boolean;
begin
  if conAutenticacion then
  begin
    compEnvioEmail.AuthenticationType := atLogin;
    compEnvioEmail.UserId := usuario;
    compEnvioEmail.Password := contrasena;
  end
  else
    compEnvioEmail.AuthenticationType := atNone;
  compMensaje := TIdMessage.Create (nil);
  compMensaje.From.Address := emisor;
  compMensaje.From.Name := nombreEmisor;
  compMensaje.Recipients.Add.Address := destinatario;
  compMensaje.CCList.Add.Address := cc;
  compMensaje.Body.AddStrings (mensaje);
  compMensaje.Subject := asunto;
  compMensaje.ReplyTo.Add.Address := emisor;
  envioCorrecto := true;
  try
    compEnvioEmail.Send(compMensaje);
  except
    envioCorrecto := false;
  end;
  compMensaje.Free;
  enviarEmail := envioCorrecto;
end;

procedure TformEnvioComentarios.bCerrarClick(Sender: TObject);
begin
  close;
end;

procedure TformEnvioComentarios.bEnviarClick(Sender: TObject);
var
  mensajeTexto : TStringList;
  i : integer;
begin
  mensajeTexto := TStringList.Create;
  mensajeTexto.Add ('Comentario enviado por: ' + txtNombre.Text);
  mensajeTexto.Add ('Email: ' + txtEmail.Text);
  mensajeTexto.Add ('Día y hora: ' + DateToStr(Date) + ' ' + TimeToStr(Time));
  mensajeTexto.Add(' ');
  mensajeTexto.Add('Comentario: ');
  for i := 0 to txtComentario.lines.count do
    mensajeTexto.Add (txtComentario.Lines[i]);

  if ConectarServidorEmail ('servidordeemail', 'usuario', 25) then
  begin
    if enviarEmail('servidordeemail', 'usuario', 'contraseña', 25,
        'Comentario enviado vía email', mensajeTexto, true, txtEmail.Text,
        txtNombre.Text, 'emaildestinatario1', 'emaildestinatario2') then
      MessageDlg ('El comentario ha sido enviado correctamente, en breve ' +
          'nos pondremos en contacto con usted. Muchas gracias por su interés.',
          mtinformation, [mbok], 0)
    else
      MessageDlg ('Ha habido un error al intentar enviar el comentario. ' +
          'Compruebe que tiene conexión a Internet e inténtelo de nuevo.',
          mtwarning, [mbok], 0)
  end
  else
    MessageDlg ('No se ha podido conectar con el servidor de email. ' +
      'Compruebe que tiene conexión a Internet e inténtelo de nuevo.',
      mtwarning, [mbok], 0);
end;

end.
Obviamente deberemos cambiar los datos del servidor de email, el usuario, la contraseña y el email de destino.




Publicado el: 2005-07-07

Visita nuestro nuevo sitio web con programas y contenidos actualizados: Proyecto A