Ver Mensaje Individual
  #24  
Antiguo 26-11-2010
elguille elguille is offline
Miembro
 
Registrado: ene 2005
Posts: 114
Reputación: 20
elguille Va por buen camino
Una función sin componentes declarados resumiendo lo dicho para poner en cualquier lado
Código Delphi [-]
Uses
  Forms, db, dbTables, dbGrids, Classes, Messages, Buttons,
  StdCtrls, Controls, Dialogs, SysUtils, IniFiles, Wintypes,
  DBIProcs, DBITypes, DBIErrs, ComCtrls, ExtCtrls, Printers,
  Windows, ShlObj, Ole2, Registry, quickrpt, qrctrls, Grids, shellapi, comobj,
  Variants,
  dbctrls, IdHTTP, WinInet, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
  IdSSL, IdSSLOpenSSL, IdExplicitTLSClientServerBase,
  IdBaseComponent, IdComponent, IdTCPServer, IdSMTPServer,
  IdMessage, IdEMailAddress, IdTCPConnection, IdTCPClient, IdMessageClient,
  IdSMTP, idexception, myaccess, IdAttachmentFile;

  procedure emailgmail(host: string; nombre: string; password: string;
    port: integer; desde: string; hasta: string; asunto: string;
    cuerpo: string; adjunto: string);
  var
    IDSMTP1: TIDSMTP; // Componente de envio de correo
    IdSSLIOHandlerSocketOpenSSL: tIdSSLIOHandlerSocketOpenSSL;
    mensaje: TIdMessage;
  begin
    // ejemplo emailgmail('smtp.gmail.com',*usuario*,*contraseña*,465,*emailorigen*,*emaildestino*,*asunto*,*cuerpo  *,*ficheroadjunto*);
    IdSSLIOHandlerSocketOpenSSL := tIdSSLIOHandlerSocketOpenSSL.Create(nil);
    IdSSLIOHandlerSocketOpenSSL.Destination := 'smtp.gmail.com:465';
    IdSSLIOHandlerSocketOpenSSL.host := 'smtp.gmail.com:465';
    IdSSLIOHandlerSocketOpenSSL.port := 465;
    IdSSLIOHandlerSocketOpenSSL.ssloptions.method := sslvSSLv3;
    IdSSLIOHandlerSocketOpenSSL.ssloptions.mode := sslmUnassigned;
    IDSMTP1 := TIDSMTP.Create(nil);
    IDSMTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL;
    IDSMTP1.UserNAME := nombre;
    IDSMTP1.password := password;
    IDSMTP1.host := host;
    IDSMTP1.port := port;
    IDSMTP1.UseTLS := utUseImplicitTLS;
    mensaje := TIdMessage.Create(nil);
    with mensaje do
    begin
      Body.Add(cuerpo);
      From.Text := desde;
      Recipients.EMailAddresses := hasta;
      Subject := asunto;
      Priority := TIdMessagePriority(mpHighest); // prioridad del mensaje
      CCList.EMailAddresses := '';
      BccList.EMailAddresses := '';
      if adjunto <> '' THEN
        TIdAttachmentfile.Create(mensaje.MessageParts, adjunto);
    end;
    with IDSMTP1 do
    begin
      try
        Connect;
        try
          // Envio el mensaje.
          Send(mensaje);
        finally
          Disconnect;
        end;
        // MessageDlg('Enviado Correctamente', mtInformation, [mbOK], 0);
      except
        // Capturo algunas excepciones que pueden ocurrir (alguien podria ampliarlo con los posibles errores)
        { ON E: EProtocolReplyError do begin
          MessageDlg('No se ha podido enviar el email.' + #13 +
          'Incorrecto el email o el usuario o la password.', mtError, [mbOK], 0);
          end; }
        on E: EFOpenError do
        begin
          MessageDlg('No se ha podido enviar el email.' + #13 +
              'Fichero Adjunto desconocido o erróneo.', mtError, [mbOK], 0);
        end;
        { on E: EIdSocketError do begin
          MessageDlg('No se ha podido enviar el email.'+ #13 +
          'Host desconocido o incorrecto.', mtError, [mbOK], 0);
          end }
        else
          begin
            MessageDlg('Fallo en el envio de email', mtError, [mbOK], 0);
          end;
        end;
      end;
      IDSMTP1.free;
      IdSSLIOHandlerSocketOpenSSL.free;
    ENd;
ejemplo sustituyendo lo encerrado en * por vuestras necesidades

emailgmail('smtp.gmail.com',*usuario*,*contraseña*,465,*emailorigen*,*emaildestino*,*asunto*,*cuerpo *,*ficheroadjunto*);

(no os olvideis de copiar las dll de OPENSSL mencionadas en el post accesibles al ejecutable)

Delphi 2010 y las Indy que vienen con el

Última edición por elguille fecha: 26-11-2010 a las 18:29:16.
Responder Con Cita