Resumo todo lo dicho con una funcion que funciona
(comprobado)
añadir a USES
IdBaseComponent, IdComponent, IdTCPServer, IdSMTPServer,
IdMessage, IdEMailAddress, IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP,idexception
Código Delphi
[-]
procedure email2(host:string;nombre:string;password:string;port:integer;desde:string;
hasta:string;asunto:string;cuerpo:string;adjunto:string);
var
IDSMTP1: TIDSMTP; mensaje: TIdMessage;
begin
IDSMTP1:=TIDSMTP.Create(nil);
IdSMTP1.AuthenticationType :=atLogin;
IdSMTP1.UserNAME := nombre;
IdSMTP1.Password := password;
IdSMTP1.Host := host;
IdSMTP1.Port := port;
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 := '';
TIdAttachment.Create(Mensaje.MessageParts,adjunto);
end; with idsmtp1 do
begin
try
Connect;
try
Send(Mensaje);
finally
Disconnect;
end;
except
ON E: EIdProtocolReplyError 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;
ENd;