Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 13-08-2021
Avatar de kuan-yiu
[kuan-yiu] kuan-yiu is offline
Miembro Premium
 
Registrado: jun 2006
Ubicación: Galicia. España.
Posts: 1.017
Poder: 19
kuan-yiu Va camino a la fama
¿Qué versión de la librería tienes? ¿La has actualizado recientemente? ¿La has movido de sitio?
Responder Con Cita
  #2  
Antiguo 14-08-2021
PepCat PepCat is offline
Miembro
 
Registrado: mar 2017
Posts: 96
Poder: 8
PepCat Va por buen camino
Hola,

Por si te sirve de ayuda, adjunto esta clase para enviar emails desde una cuenta de gmail (puerto 465) que facilmente es adaptable a otro servicio de mail. No te olvides de incluir las librerias openSSL en la carpeta de tu ejecutable.

Código Delphi [-]

unit cfs.Email.gmail;

(*
   Step by Step :
  1. if you have not a gmail account, Create it.
  2. Important :
   -  Enter in your Personnal Setting (gmail) go to "Security":
      Activate “2-Step Verification” and add “App Password”
      or Activate "Allow less secure apps "
  3.  Add in your executable file path the two openSSL Libraries:  "libeay32.dll"  and  "ssleay32.dll"

  More info:
  http://delphiprogrammingdiary.blogsp...format-in.html
  http://www.andrecelestino.com/delphi...ponentes-indy/

*)

(*
  Use sample:

  Gmail := TGmail.Create('YourAccount@gmail.com', 'App password', 'From you/company');
  try
    try
      Gmail.Connect;
      Gmail.Send(['useremail@gmail.com'], 'Subject', 'PlainBody', 'htmlBody', 'AttachmentFile');
      Gmail.Send(...);
      Gmail.Send(...);
    except
      on E: Exception do
        ShowMessage(E.Message);
    end;
  finally
    GEmail.Free;
  end;
*)

interface

uses
  System.SysUtils, System.Classes, IdTCPConnection, IdExplicitTLSClientServerBase, IdMessageClient,  IdSMTP, IdMessage, IdIOHandler,
  IdBaseComponent, IdComponent, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdText, IdAttachmentFile;

type
  TcfsGmail = class
  private
    FFromName: string;
    FIdSSLIOHandlerSocket: TIdSSLIOHandlerSocketOpenSSL;
    FIdSMTP: TIdSMTP;
  public
    constructor Create(const UserName, Password, FromName: string);
    destructor Destroy; override;
    procedure Connect;
    procedure Send(ToAddresses: array of string; const Subject, PlainBody: string; const HTMLBody: string = ''; const AttachmentFile: string = '');
 end;

implementation


constructor TcfsGmail.Create(const UserName, Password, FromName: string);
begin
  FFromName := FromName;

  FIdSSLIOHandlerSocket := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  FIdSSLIOHandlerSocket.SSLOptions.Method := sslvSSLv23;
  FIdSSLIOHandlerSocket.SSLOptions.Mode := sslmClient;
  FIdSSLIOHandlerSocket.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];

  FIdSMTP := TIdSMTP.Create(nil);
  FIdSMTP.IOHandler := FIdSSLIOHandlerSocket;
  FIdSMTP.UseTLS := utUseImplicitTLS;
  FIdSMTP.AuthType := satDefault;

  FIdSMTP.Host := 'smtp.gmail.com';
  FIdSMTP.Port := 465;

  FIdSMTP.Username := UserName;
  FIdSMTP.Password := Password;
end;

destructor TcfsGmail.Destroy;
begin
  if Assigned(FIdSMTP) then
  begin
    try
      FIdSMTP.Disconnect;
    except
    end;
    UnLoadOpenSSLLibrary;
    FreeAndNil(FIdSMTP);
  end;
  if Assigned(FIdSSLIOHandlerSocket) then
    FreeAndNil(FIdSSLIOHandlerSocket);

  inherited;
end;

procedure TcfsGmail.Connect;
begin
  FIdSMTP.Connect;
  FIdSMTP.Authenticate;
end;

procedure TcfsGmail.Send(ToAddresses: array of string; const Subject, PlainBody: string; const HTMLBody: string = ''; const AttachmentFile: string = '');
var
  IdMessage: TIdMessage;
  IdText: TIdText;
  Address: string;
  AttachFileExist: Boolean;
  MultipartAlternative: Boolean;
begin
  if not FIdSMTP.Connected then
    Connect;

  IdMessage := TIdMessage.Create(nil);
  try
    IdMessage.From.Address := FIdSMTP.Username;
    IdMessage.From.Name := FFromName;

    for Address in ToAddresses do
    begin
      if Address <> '' then
        IdMessage.Recipients.Add.Text := Address;
    end;

    IdMessage.Subject := Subject;

    AttachFileExist := False;
    if AttachmentFile <> '' then
      AttachFileExist := FileExists(AttachmentFile);

    MultipartAlternative := False;
    if (PlainBody <> '') and (HTMLBody <> '')  then
      MultipartAlternative := True;

    IdMessage.ContentType := 'multipart/alternative';
    if AttachFileExist then
    begin
      if MultipartAlternative then
        IdMessage.ContentType := 'multipart/related; type="multipart/alternative"'
      else
        IdMessage.ContentType := 'multipart/mixed';
    end;

    if MultipartAlternative and AttachFileExist then
    begin
      IdText := TIdText.Create(IdMessage.MessageParts);
      IdText.ContentType := 'multipart/alternative';
    end;

    // plain body
    if PlainBody <> '' then
    begin
      IdText := TIdText.Create(IdMessage.MessageParts);
      IdText.ContentType := 'text/plain; charset="UTF-8"';
      IdText.Body.Text := PlainBody;
      if MultipartAlternative and AttachFileExist then
        IdText.ParentPart := 0;
    end;

    // html body
    if HTMLBody <> '' then
    begin
      IdText := TIdText.Create(IdMessage.MessageParts);
      IdText.ContentType := 'text/html; charset="UTF-8"';
      IdText.Body.Text := HTMLBody;
      if MultipartAlternative and AttachFileExist then
        IdText.ParentPart := 0;
    end;

    if AttachFileExist then
      TIdAttachmentFile.Create(IdMessage.MessageParts, AttachmentFile);

    FIdSMTP.Send(IdMessage);
  finally
    IdMessage.Free;
  end;
end;

end.
Responder Con Cita
  #3  
Antiguo 15-08-2021
usuario1000 usuario1000 is offline
Miembro
 
Registrado: nov 2016
Posts: 86
Poder: 8
usuario1000 Va por buen camino
Solucionado!!!! muchisimas gracias, me ha servido mucho tu código. Enormemente agradecido.

Última edición por Casimiro Notevi fecha: 15-08-2021 a las 20:22:53.
Responder Con Cita
  #4  
Antiguo 15-08-2021
usuario1000 usuario1000 is offline
Miembro
 
Registrado: nov 2016
Posts: 86
Poder: 8
usuario1000 Va por buen camino
Muchas gracias a todos, lo he solucionado con el código de PepCat.



Enormemente agradecido.


Gracias y cuidense!!!!!
Responder Con Cita
  #5  
Antiguo 15-08-2021
usuario1000 usuario1000 is offline
Miembro
 
Registrado: nov 2016
Posts: 86
Poder: 8
usuario1000 Va por buen camino
Cita:
Empezado por kuan-yiu Ver Mensaje
¿Qué versión de la librería tienes? ¿La has actualizado recientemente? ¿La has movido de sitio?
No he tocado nada, estaba el programa funcionando perfectamente y de pronto ha dejado de funcionar(la parte enviar email). Creo que se debe, por sospechar de algo, a una actualización de windows que se ha hecho recientemente.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Enviar web por email fherwashere Internet 3 13-04-2011 04:03:02
Ayuda con Email ekbadel PHP 1 05-10-2010 12:06:55
email con php BuenaOnda PHP 3 15-08-2007 22:22:47


La franja horaria es GMT +2. Ahora son las 10:27:22.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi