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; IdSSLIOHandlerSocketOpenSSL: tIdSSLIOHandlerSocketOpenSSL;
mensaje: TIdMessage;
begin
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); CCList.EMailAddresses := '';
BccList.EMailAddresses := '';
if adjunto <> '' THEN
TIdAttachmentfile.Create(mensaje.MessageParts, adjunto);
end;
with IDSMTP1 do
begin
try
Connect;
try
Send(mensaje);
finally
Disconnect;
end;
except
on E: EFOpenError do
begin
MessageDlg('No se ha podido enviar el email.' + #13 +
'Fichero Adjunto desconocido o erróneo.', 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