E-mail HTML-file (in body) with images

Giganews Newsgroups
Subject: E-mail HTML-file (in body) with images
Posted by:  Stef Merlijn (merlijn-…@hccnet.nl)
Date: Wed, 23 Jan 2008

Hi,

After I entered the content of the E-mail-body in a TWPRichText-editor
(including formatted text like bold, italic, etc) it is saved as a
HTML-file and one or more image-files. Each filename is store in a
TStringList => EmailFiles.

What I would like to know is how I can add the HTML-file in the body of
an E-mail including the images and send it. On the forum I already
found the basics for this, but I can't get it to work. Below you will
find the code for sending the E-mail (which is correct).

procedure TFSendMail.btSendMailClick(Sender: TObject);
var i : Integer;
    s : string;
begin
  // Create temporary file name
  i := 0;
  repeat
    s := Format('%sMessage%d.htm', [EmailDir,i]);
    inc(i);
  until not FileExists(s);

  // Save the text as HTML file
  EmailFiles.Clear;
  EmailFiles.Add(s);
  WPRichText.SaveToFile(s);

  // Now we let the e-mail library send all files in the list EmailFiles
  SendEmailMessage( WPRichText.ASANSIString('ANSI'), EmailFiles );
  // and clear the files
  for i := EmailFiles.Count -1 downto 0 do
    DeleteFile(EmailFiles[i]);
  EmailFiles.Clear;
end;

procedure TFSendMail.SendEmailMessage(body : string; Attachments :
TStringList);
var attachimg : TIdAttachmentFile;
    emptybody, textbody, htmlbody : TIdText;
begin
  emptybody := TIdText.Create(MailMessage.MessageParts, nil);
  emptybody.ContentType := 'multipart/alternative';
  emptybody.ParentPart := -1;

  textbody := TIdText.Create(MailMessage.MessageParts, nil);
  textbody.Body.Text := '';
  textbody.ContentType := 'text/plain';
  textbody.ParentPart := 0;

  htmlbody := TIdText.Create(MailMessage.MessageParts, nil);
  htmlbody.body.text := body;      // The HTML-file
  htmlbody.ContentType := 'text/html';
  htmlbody.ParentPart := 0;

  Attachimg := TIdAttachmentFile.Create(MailMessage.MessageParts,
PChar(EmailFiles);
  Attachimg.ContentID                  := '<12345>';
  Attachimg.FileIsTempFile              := False;  // Bestand niet
verwijderen na verzending.
  Attachimg.ContentDisposition          := 'inline';
  Attachimg.ContentType                := 'image/jpeg';
  Attachimg.ParentPart                  := -1;

  MailMessage.ContentType              := 'multipart/related;
type="multipart/alternative"';
  MailMessage.CharSet                  := 'iso-8859-1';
  MailMessage.Encoding                  := meMIME;
  MailMessage.From.Address              := 'ma…@mymail.nl';
  MailMessage.From.Name                := 'My name';
  MailMessage.Recipients.EMailAddresses := 'in…@mymail.nl';
  MailMessage.Subject                  := 'Test';

  Mail.Username := '…@mymail.nl';
  Mail.Password := 'secretcode';
  Mail.Host    := 'mail.mymail.nl';
  Mail.Port    := 25;

  try
    Mail.Connect();
    try
      Mail.Send(MailMessage);
    except
      on E: Exception do
        showmessage('Send Failed ' + E.Message);
    end;
  finally
    MailMessage.MessageParts.Clear;
    MailMessage.Headers.Clear;
    Mail.Disconnect;
    showmessage('Send Successful');
  end;
end;

Can anybody help me getting this to work?

Thank you very much.

Replies