var
mail : TIdMessage;
adjuntos : TStrings;
i,x : integer;
begin
if txtServidorSMTP.Text = '' then
begin
MessageDlg ('Debe indicar el la dirección del servidor de SMTP.', mtWarning, [mbok], 0);
tabDatos.Show;
exit;
end;
if (lsAutenticacion.Text <> 'Ninguna') and ((txtUsuario.Text = '') or (txtContrasena.Text = '')) then
begin
MessageDlg ('Debe indicar el usuario y contraseña de autenticación del servidor SMTP.', mtWarning, [mbok], 0);
tabDatos.Show;
exit;
end;
if lsTLS.Text = '' then
begin
MessageDlg ('Debe indicar si requiere TLS / SSL.', mtWarning, [mbok], 0);
tabDatos.Show;
exit;
end;
if txtPuerto.Text = '' then
begin
MessageDlg ('Debe indicar el puerto de envío de mail (por defecto para TLS ' + 'es 587 y sin TLS es 25).', mtWarning, [mbok], 0);
tabDatos.Show;
exit;
end;
if txtRemitenteNombre.Text = '' then
begin
MessageDlg ('Debe indicar el nombre del remitente.', mtWarning, [mbok], 0);
tabDatos.Show;
exit;
end;
if txtRemitenteMail.Text = '' then
begin
MessageDlg ('Debe indicar el e-mail del remitente.', mtWarning, [mbok], 0);
tabDatos.Show;
exit;
end;
if txtDestinatario.Text = '' then
begin
MessageDlg ('Debe indicar el e-mail del destinatario (para varios mails separar con comas).',
mtWarning, [mbok], 0);
tabEnviarMail.Show;
exit;
end;
if txtAsunto.Text = '' then
begin
MessageDlg ('Debe indicar el asunto del correo electrónico.', mtWarning, [mbok], 0);
tabEnviarMail.Show;
exit;
end;
if txtMensaje.Text = '' then
begin
MessageDlg ('Debe indicar el cuerpo del correo electrónico.', mtWarning, [mbok], 0);
tabEnviarMail.Show;
exit;
end;
if (pos('adjunto', AnsiLowerCase(txtMensaje.Text)) > 0) and (lFicherosAdjuntos.Count = 0) then
begin
if MessageDlg ('Ha introducido la palabra "adjunto" en el cuerpo pero no ha adjuntado documentos ' +
'al correo electrónico ¿Desea continuar con el envío sin adjuntos?',
mtConfirmation, [mbYes, mbNo], 0) = mrno then
begin
tabEnviarMail.Show;
exit;
end;
end;
with idSMTP do
begin
if lsMetdoSSL.Text = 'sslvTLSv1' then
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvTLSv1;
if lsMetdoSSL.Text = 'sslvTLSv2' then
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvSSLv2;
if lsMetdoSSL.Text = 'sslvTLSv23' then
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvSSLv23;
if lsMetdoSSL.Text = 'sslvTLSv3' then
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvSSLv3;
IoHandler := IdSSLIOHandlerSocketOpenSSL1;
if lsAutenticacion.Text = 'Defecto' then
AuthType := atDefault;
if lsAutenticacion.Text = 'Ninguna' then
AuthType := atNone;
if lsAutenticacion.Text = 'SASL' then
AuthType := atSASL;
Username := txtUsuario.Text;
Password := txtContrasena.Text;
Host := txtServidorSMTP.Text;
if lsTLS.Text = 'TLS explícito' then
useTLS := utUseExplicitTLS;
if lsTLS.Text = 'Sin soporte TLS' then
useTLS := utNoTLSSupport;
if lsTLS.Text = 'TLS implícito' then
useTLS := utUseExplicitTLS;
if lsTLS.Text = 'TLS requerido' then
useTLS := utUseExplicitTLS;
port := strtoint(txtPuerto.Text);
end;
try
txtResultado.Lines.add(DateTimeToStr(now) + ' Conectando con el servidor ' + txtServidorSMTP.Text);
idSMTP.Connect;
txtResultado.Lines.add(DateTimeToStr(now) + ' Conectado a ' + txtServidorSMTP.Text);
except
on e : exception do
begin
txtResultado.Lines.add(DateTimeToStr(now) + ' Error al conectar al servidor SMTP. ' + e.ClassName + ' ' + e.Message);
MessageDlg('Error al conectar con servidor SMTP: ' + e.Message, mtError, [mbok], 0);
end;
end;
mail := TIdMessage.Create(Self);
with mail do
begin
From.Name := txtRemitenteNombre.Text;
From.Address := txtRemitenteMail.Text;
Recipients.EMailAddresses := txtDestinatario.Text;
CCList.EMailAddresses := txtCC.Text;
BccList.EMailAddresses := txtCCO.Text;
Subject := txtAsunto.Text;
body.Text := txtMensaje.Text;
adjuntos := lFicherosAdjuntos.Items;
for i := 0 to adjuntos.Count - 1 do
begin
if FileExists(adjuntos.Strings[i]) then
begin
TIdAttachmentFile.Create(MessageParts, adjuntos.Strings[i])
end;
end;
end;
try
idSMTP.Send(mail);
txtResultado.Lines.add(DateTimeToStr(now) + ' Mensaje enviado correctamente ('+inttostr(x)+')');
MessageDlg('Mensaje enviado correctamente.', mtInformation, [mbok], 0);
if idSMTP.Connected then idSMTP.Disconnect;
except
on e : exception do
begin
txtResultado.Lines.add(DateTimeToStr(now) + ' Error al enviar mensaje. ' + e.ClassName + ' ' + e.Message);
MessageDlg('Error al enviar e-mail: ' + e.Message, mtError, [mbok], 0);
if idSMTP.Connected then
idSMTP.Disconnect;
end;
end;