Ver Mensaje Individual
  #9  
Antiguo 04-03-2014
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
Club Delphi,

Cita:
Empezado por José Luis Garcí
...podéis facilitarme el trozo de código que manda un email con un adjunto...
Revisa este código:
Código Delphi [-]
unit UfrmPrincipal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,

  IdIOHandler, IdIOHandlerSocket, IdSSLOpenSSL, IdMessage,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdMessageClient, IdSMTP;

type
  TfrmPrincipal = class(TForm)
    mCuerpoEmail: TMemo;
    edAsuntoEmail: TEdit;
    edServidorSmtp: TEdit;
    lbAsuntoEmail: TLabel;
    lbCuerpoEmail: TLabel;
    lbServidorSmtp: TLabel;
    edNombreUsuario: TEdit;
    btnEnviarEmail: TButton;
    lbNombreUsuario: TLabel;
    edEmailRemitente: TEdit;
    lbEmailRemitente: TLabel;
    edContrasenaUsuario: TEdit;
    edEmailDestinatario: TEdit;
    lbEmailDestinatario: TLabel;
    lbContrasenaUsuario: TLabel;
    IdSMTP: TIdSMTP;
    IdMessage: TIdMessage;
    IdSSLIOHandlerSocket: TIdSSLIOHandlerSocket;
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    procedure btnEnviarEmailClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  end;

var
  frmPrincipal: TfrmPrincipal;
  Adjunto : String;

implementation

{$R *.dfm}

procedure TfrmPrincipal.btnEnviarEmailClick(Sender: TObject);
begin

  with IdMessage do
  begin

     Body.Clear();
     Recipients.Clear();
     Sender.Address := edEmailRemitente.Text;
     Recipients.EMailAddresses := edEmailDestinatario.Text;
     Subject := edAsuntoEmail.Text;
     Body.AddStrings(mCuerpoEmail.Lines);

     if FileExists(Adjunto) then
        with TIdAttachment.Create(MessageParts,Adjunto) do
        begin
           ContentType := 'binary';
        end

  end;

  with IdSMTP do
  begin

     IOHandler := IdSSLIOHandlerSocket;
     IdSSLIOHandlerSocket.SSLOptions.Method := sslvSSLv23; // sslv : SSLv2, SSLv3, TLSv1, SSLv23.
     IdSSLIOHandlerSocket.SSLOptions.Mode := sslmUnassigned;
     IdSSLIOHandlerSocket.SSLOptions.VerifyMode := [];
     IdSSLIOHandlerSocket.SSLOptions.VerifyDepth := 0;
     Port := 465;
     Host := edServidorSmtp.Text;
     AuthenticationType := atLogin;
     Username := edNombreUsuario.Text;
     Password := edContrasenaUsuario.Text;

     try
        Connect(5000);
     except
        raise Exception.Create('Error de Conexión');
     end;

     btnEnviarEmail.Enabled := false;
     try
        Send(IdMessage);
        ShowMessage('Correo enviado con éxito');
     finally
        btnEnviarEmail.Enabled := true;
     end;

     if Connected then
        Disconnect();

  end;

end;

procedure TfrmPrincipal.Button1Click(Sender: TObject);
begin
   if openDialog1.Execute then
      adjunto := openDialog1.Filename;
end;

end.
El código anterior envía un email con su adjunto vía Gmail con Delphi 7 bajo Windows 7 Professional x32, Indy 9.00.10 y la librería ssl indy_openssl096 (libeay32.dll y ssleay32.dll).

La librería indy_openssl096 esta disponible en : indy_openssl096.zip

Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 04-03-2014 a las 16:52:26.
Responder Con Cita