Ver Mensaje Individual
  #6  
Antiguo 13-10-2007
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.107
Reputación: 34
dec Tiene un aura espectaculardec Tiene un aura espectacular
Hola,

El siguiente código lo extraigo de un ejemplo que guardo por aquí con el nombre "Enviar correos HTML con imágenes", más o menos. A ver si te sirve para hacerte una idea. El asunto principal es el "ContentType" del mensaje de correo. Pero, piensa que el cliente de correo que lo reciba ha de admitir el HTML. Por ejemplo, el que yo uso no lo admite, sólo ve texto plano (afortunadamente) y así hay mensajes que ni veo...

Pero fíjate que el código siguiente muestra la que quizá sea la forma adecuada de hacerlo: enviar el correo en texto plano y en HTML, de modo que el cliente pueda elegir, o, por lo menos, no quedarse sin mensaje si no puede leer HTML.

Código Delphi [-]
uses
  idMessage;

procedure TForm1.Button1Click(Sender: TObject);
var
  html: TStrings;
  htmpart, txtpart: TIdText;
  bmppart: TIdAttachment;
  email: TIdMessage;
  filename: string;
begin
  filename := ExtractFilePath(Application.ExeName) + 'us.jpg';

  html := TStringList.Create();
  html.Add('< html >');
  html.Add('< head >');
  html.Add('< /head >');
  html.Add('< body >< h1 >Hello< /h1 >');
  html.Add('< img src="cid:us.jpg" / >');
  html.Add('This is a picture of us!');
  html.Add('< /html >');

  email := TIdMessage.Create(nil);
  email.From.Text := 'jhondoe@jhondoe.com';
  email.Recipients.EMailAddresses := 'jhondoeson@jhondoeson.com';
  email.Subject := 'Hello';
  email.ContentType := 'multipart/mixed';
  email.Body.Assign(html);

  txtpart := TIdText.Create(email.MessageParts);
  txtpart.ContentType := 'text/plain';
  txtpart.Body.Text := '';

  htmpart := TIdText.Create(email.MessageParts, html);
  htmpart.ContentType := 'text/html';

  bmppart := TIdAttachment.Create(email.MessageParts, filename);
  bmppart.ContentType := 'image/jpeg';
  bmppart.FileIsTempFile := true;
  bmppart.ContentDisposition := 'inline';
  bmppart.ExtraHeaders.Values['content-id'] := 'us.jpg';
  bmppart.DisplayName := 'us.jpg';

  try
    idSMTP.Connect();
    try
      idSMTP.Send(email);
      ShowMessage('Sent');
    except
      on E: Exception do
        ShowMessage('Failed: ' + E.Message);
    end;
  finally
    idSMTP.Disconnect();
    email.Free();
    html.Free();
  end;
end;

Nota: Fíjate que el HTML que aparece en el código tiene etiquetas "con espacios". Esto es sólo para que se vea bien el código en los foros, en realidad las etiquetas no llevan espacios.
__________________
David Esperalta
www.decsoftutils.com

Última edición por dec fecha: 13-10-2007 a las 13:10:47.
Responder Con Cita