Read E-Mail

Is there a tool to read E-Mails from a IMAP Account (T-Online) and save the Atachment to a folder.
(Not from Outlook).

Hi Gerald,
i do not know if tms have something to work with email.
In my case i use one library that is included in getit with delphi 11 & 12 from nsoftware called IPWORKS. With it i work with yahoo account as pop3 and/or imap with or without attachment(s).
Until our TMS staff does not write a package for email, you can work with it. Just get the message, check if there is/are attachment(s), get it and save it on your folder.
Have a nice day
Daniele

I use this example, but cant connect to my account at
"IMAP.Connect".
It waits the Timeout Time and then goes to
"IMAP.Disconnect;
ListBox1.Items.Add('Verbindung zum IMAP-Server getrennt.');"

I had hoped that TMS would have a tool that would make connecting and processing a little easier. I don't want to buy another tool in addition to TMS-all-Access.

This is my example:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  IdIMAP4, IdSSLOpenSSL, IdMessage, IdAttachmentFile;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure DownloadAttachmentsFromTOnline;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DownloadAttachmentsFromTOnline;
var
  IMAP: TIdIMAP4;
  SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
  Msg: TIdMessage;
  Attachment: TIdAttachmentFile;
  I, J: Integer;
  SaveDir: string;
begin
  IMAP := TIdIMAP4.Create(nil);
  SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  Msg := TIdMessage.Create(nil);
  try
    // Initialisieren Sie die OpenSSL-Bibliotheken
    if not IdSSLOpenSSL.LoadOpenSSLLibrary then
    begin
      raise Exception.Create('Fehler beim Laden der OpenSSL-Bibliotheken.');
    end;

    // Konfigurieren Sie die SSL-Handler
    SSLHandler.SSLOptions.Method := sslvTLSv1_2;
    SSLHandler.SSLOptions.Mode := sslmUnassigned;
    SSLHandler.SSLOptions.VerifyMode := [];
    SSLHandler.SSLOptions.VerifyDepth := 0;

    // Konfigurieren Sie die IMAP-Verbindung
    IMAP.IOHandler := SSLHandler;
    IMAP.Host := 'secureimap.t-online.de';
    IMAP.Port := 993;
    IMAP.Username := 'xxxx@t-online.de';
    IMAP.Password := '123456789';

    // Erhöhen Sie die Zeitüberschreitungseinstellungen
    IMAP.ConnectTimeout := 5000; // 5 Sekunden
    IMAP.ReadTimeout := 5000;    // 5 Sekunden

    // Verbinden und Anmelden
    try
      ListBox1.Items.Add('Verbinden mit dem IMAP-Server...');
      IMAP.Connect;
      ListBox1.Items.Add('Verbunden mit dem IMAP-Server.');
    except
      on E: Exception do
      begin
        ListBox1.Items.Add('Fehler beim Verbinden mit dem IMAP-Server: ' + E.Message);
        raise;
      end;
    end;

    try
      // Wählen Sie den Posteingang
      IMAP.SelectMailBox('INBOX');
      ListBox1.Items.Add('Posteingang ausgewählt.');

      // Durchsuchen Sie alle E-Mails im Posteingang
      for I := 0 to IMAP.MailBox.TotalMsgs - 1 do
      begin
        // Laden Sie die E-Mail
        IMAP.Retrieve(I, Msg);
        ListBox1.Items.Add('E-Mail geladen: ' + Msg.Subject);

        // Verarbeiten Sie die Anhänge
        SaveDir := 'C:\Projekte\Test\ReadMail\Files\';
        if Msg.MessageParts.Count > 0 then
        begin
          for J := 0 to Msg.MessageParts.Count - 1 do
          begin
            if Msg.MessageParts[J] is TIdAttachmentFile then
            begin
              Attachment := TIdAttachmentFile(Msg.MessageParts[J]);
              Attachment.SaveToFile(SaveDir + Attachment.FileName);
              ListBox1.Items.Add('Anhang gespeichert: ' + Attachment.FileName);
            end;
          end;
        end;
      end;
    finally
      IMAP.Disconnect;
      ListBox1.Items.Add('Verbindung zum IMAP-Server getrennt.');
    end;
  finally
    IMAP.Free;
    SSLHandler.Free;
    Msg.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items.Clear;
  try
    DownloadAttachmentsFromTOnline;
  except
    on E: Exception do
    begin
      ListBox1.Items.Add('Fehler: ' + E.Message);
    end;
  end;
end;

end.


Hi Gerald,
in my program i use Indy and IPWorks to send email with pop3 and imap.
This because i had (and have) a lot of trouble with indy and imap.
I tried to solve the problems via indy forum .... without any result.
I suggest you to try IPWorks that is available in delphi 11 and 12 via get it and for delphi enterprise version is a full product.
I'm sure that there are other free library but not all work with imap.
As soon as possiblie i extract a workable piece of code with ipworks in order to connect and do somthing with imap.
Have a nice day.
Best regards
Daniele

Thank you for helping me.
I installed ipWorks and would try to test it asap.
It would be nice if you would help me to start up with an example to connect and how to read messages and attachments.

Hi Gerald,
sorry for delay ...
I'm quite busy (we are few on work ...... ) but, if you are still interested, within a couple of day i can send here a little demo program ...

Excuse me for delay ...

Regards
Daniele

Yes it would be nice if you could help me, i have try somthing but the reading of the header and the mail seems not to do in the way i tried. it works but i think it should be done in the events.
Here some code:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, ipwcore, ipwtypes, ipwimap, ipwsyslog, VCL.TMSFNCTypes,
  VCL.TMSFNCUtils, VCL.TMSFNCGraphics, VCL.TMSFNCGraphicsTypes, VCL.TMSFNCScrollControl, VCL.TMSFNCRichEditorBase,
  VCL.TMSFNCRichEditor, VCL.TMSFNCCustomComponent, VCL.TMSFNCRichEditorIO, ipwhtmlmailer;

type
  TfrmMain = class(TForm)
    btnConnect: TButton;
    btnFetch: TButton;
    Mail: TipwIMAP;
    ipwHTMLMailer1: TipwHTMLMailer;
    TMSFNCRichEditorHTMLIO1: TTMSFNCRichEditorHTMLIO;
    Memo1: TTMSFNCRichEditor;
    procedure btnConnectClick(Sender: TObject);
    procedure btnFetchClick(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure ConnectToIMAP;
    procedure FetchInboxEmails;
  public
    { Public-Deklarationen }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.btnConnectClick(Sender: TObject);
begin
  ConnectToIMAP;
end;

procedure TfrmMain.ConnectToIMAP;
begin
  Mail.MailServer := 'secureimap.t-online.de'; 
  Mail.MailPort := 993;
  Mail.User := 'xxxx@t-online.de';
  Mail.Password := '123456789';
  Mail.SSLEnabled := True;
  try
    Mail.Connect;
    // ShowMessage('Verbindung erfolgreich!');
    FetchInboxEmails;
  except
    on E: Exception do
      ShowMessage('Verbindungsfehler: ' + E.Message);
  end;
end;

procedure TfrmMain.FetchInboxEmails;
var
  i,Anzahl: Integer;
begin
  Memo1.Clear;

  try
    Mail.Mailbox := 'INBOX';
//    Mail.SelectMailbox; //Mails lesen im Read/Write Mode
    Mail.ExamineMailbox; //Mails lesen im Read only Mode
    Anzahl:=Mail.MessageCount;

    Memo1.Text:='E-Mail Anzahl: ' + IntToStr(Anzahl)+Chr(13);
    Memo1.Text:=Memo1.Text+'---------------------------'+Chr(13);

    Mail.MessageSet := '1:'+IntToStr(Anzahl);
    Mail.RetrieveMessageInfo;
    Mail.RetrieveMessageHeaders;
    Mail.RetrieveMessageText;
    Memo1.BeginUpdate;
    for i := 1 to Anzahl do
    begin
      Memo1.Text:=Memo1.Text+'Von: ' + Mail.MessageFrom+Chr(13)+Chr(13);
      Memo1.Text:=Memo1.Text+'An: ' + Mail.MessageHeaderValue[8]+Chr(13)+Chr(13);
      Memo1.Text:=Memo1.Text+'Nachricht: ' + Mail.MessageText+Chr(13);
      Memo1.Text:=Memo1.Text+'---------------------------'+Chr(13)+Chr(13);
    end;
    Memo1.EndUpdate;
  except
    on E: Exception do
      ShowMessage('Fehler beim Abrufen: ' + E.Message);
  end;
end;

procedure TfrmMain.btnFetchClick(Sender: TObject);
begin
  FetchInboxEmails;
end;

end.

Hi Gerald,
i'm ready with a very small test project. It is under delphi 11 and IPWorks 2020 (but could compile even 2024 IPWorks version).
The important thing to know (in my opinion ..... and it's can be wrong) is that the message (assuming work with imap) is a big box of part(s). In each part we get all that make the message, attachment(s) included.

See the test (where i used some tms components) where i tried to comment at my best.
See the ipworks component events !!!

Note: To be honest, this topic was interesting for me too because it was a good point of study on email attachments where, previously, they were only worked with pop3

Have a good day

Daniele

ImapTest_2025-05-09.zip (9.9 KB)

Thats fine, i can work with this.
I only do not understand how it works with the message text inside of the Mail.
How to to get it and how to show the Content of the Mail-Message.
Maybe in a WebBrowser Component?

Good morning Gerald,
you can get the message body with the property .Message tha can be recovered with
ipIMAP.MessageSet := MessageId; ipIMAP.RetrieveMessageText;

Have a nice day

Daniele