Retrieve and Display HTML Mail via POP3

 
This tutorial represents a POP3 client, which downloads selected Email messages from a POP3 mailbox, extracts MIME message parts, and displays the message content. If the message includes an HTML message part, this part is displayed using the TWebBrowser component. Otherwise, a text part is displayed.
 
 
procedure TForm1.btnRetrieveClick(Sender: TObject);
var
  i: Integer;
  stringToDisplay: string;
  image: TclImageBody;
  msg: TStrings;
begin
  clPop31.Server := edtServer.Text;
  clPop31.UserName := edtUserName.Text;
  clPop31.Password := edtPassword.Text;

  clPop31.Open();

  clPop31.Retrieve(StrToInt(edtMsgNo.Text), clMailMessage1);

  stringToDisplay := clMailMessage1.MessageText.Text;

  for i := 0 to clMailMessage1.Images.Count - 1 do
  begin
    image := clMailMessage1.Images[i];

    stringToDisplay := StringReplace(stringToDisplay, 'cid:' + image.ContentID,
      edtFolder.Text + image.FileName, [rfIgnoreCase, rfReplaceAll]);
  end;

  msg := TStringList.Create();
  try
    msg.Text := stringToDisplay;
    msg.SaveToFile(edtFolder.Text + 'temp.htm');
  finally
    msg.Free();
  end;

  WebBrowser1.Navigate(edtFolder.Text + 'temp.htm');

  clPop31.Close();
end;

 
function TForm1.GetUniqueFileName(const AFileName: string): string;
var
  dir, name, ext: string;
  i: Integer;
begin
  dir := edtFolder.Text;

  ext := ExtractFileExt(AFileName);
  name := Copy(AFileName, 1, Length(AFileName) - Length(ext));
  name := name + '(';
  ext  := ')' + ext;

  i := 1;
  while FileExists(dir + name + IntToStr(i) + ext) do
  begin
    Inc(i);
  end;

  Result := name + IntToStr(i) + ext;
end;

procedure TForm1.clMailMessage1SaveAttachment(Sender: TObject;
  ABody: TclAttachmentBody; var AFileName: string; var AData: TStream;
  var Handled: Boolean);
begin
  if (AFileName = '') then
  begin
    AFileName := GetUniqueFileName('.dat');
  end;

  AData := TFileStream.Create(edtFolder.Text + AFileName, fmCreate);
  Handled := True;
end;

Add Feedback