Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   eMails per idPOP3 abholen und als html speichern.. (https://www.delphipraxis.net/169093-emails-per-idpop3-abholen-und-als-html-speichern.html)

erich.wanker 28. Jun 2012 09:53

eMails per idPOP3 abholen und als html speichern..
 
Hallo Leute,

würde gerne meine Mails via TidPOP3 abholen und als html oder txt Datei speichern, falls die Mail Anhänge besitzt, sollen die auch abgespeichert werden.

Hab jetzt folgende Zeilen, aber ist das auch stabil ?

Mein Test:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdContext, IdBaseComponent, IdComponent, IdCustomTCPServer,
  IdTCPServer, IdCmdTCPServer, IdExplicitTLSClientServerBase, IdPOP3Server,
  Buttons, IdTCPConnection, IdTCPClient, IdMessageClient, IdPOP3, ComCtrls,
  IdMessage, StdCtrls, IdMessageCoder, IdMessageCoderMIME,IdText ,IdAttachment,
  IdIOHandler, IdIOHandlerStream, IdIOHandlerSocket, IdIOHandlerStack;

type
  TForm1 = class(TForm)
    SpeedButton1: TSpeedButton;
    POP: TIdPOP3;
    msg: TIdMessage;
    Memo1: TMemo;
    procedure SpeedButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.SpeedButton1Click(Sender: TObject);
var MailCount,Mailgroesse,intIndex :Integer;
    itm:TListItem;
    i:integer;
    s:string;
begin

      POP.Host := 'pop.1und1.de';
      POP.Port := 110;
      POP.Username := '++++';
      POP.Password := '+++++';
      POP.Connect;

      MailCount := POP.CheckMessages;

      if MailCount >0 then
      begin

          for intIndex := 1 to MailCount do
          begin
            msg.Clear;
            pop.Retrieve(intIndex, Msg);
           
            memo1.Lines.Append('**************************');
            memo1.Lines.Append('Von:           '+Msg.From.Text);
            memo1.Lines.Append('Antwortadresse: '+msg.from.Address );
            memo1.Lines.Append('Betreff:       '+Msg.Subject);
            memo1.Lines.Append('Priorität:     '+inttostr(ord(Msg.Priority)));
            memo1.Lines.Append('Datum:         '+datetostr(Msg.Date));
            memo1.Lines.Append('Uhrzeit:       '+TimeTostr(Msg.Date));
            memo1.Lines.Append('------------------------------------------');


           
                //Anhang
                for i := 0 to Msg.MessageParts.Count-1 do begin
                  if Msg.MessageParts.Items[i] is tIdAttachment then
                      begin
                        s := (Msg.MessageParts.Items[i] as tIdAttachment).Filename;
                        memo1.Lines.Append('Anhang:         '+s);
                       (Msg.MessageParts.Items[i] as tIdAttachment).savetofile(ExtractFilePath(Application.ExeName) +'/Mails/Anhang_' + IntToStr(intIndex) +s);
                      end;
                end;


                // HTML Mail
                if msg.MessageParts.Count > 0 then
                begin
                memo1.Lines.Add('HTML-Mail:');
                for i := 0 to Msg.MessageParts.Count-1 do begin
                    if Msg.MessageParts.Items[i] is TIdText then
                     begin
                      memo1.Lines.AddStrings(TIdText(Msg.MessageParts.Items[i]).Body);
                      TIdText(Msg.MessageParts.Items[i]).Body.SaveToFile(ExtractFilePath(Application.ExeName) +'/Mails/html_' + IntToStr(intIndex) + '.html');
                     end;
                end;
                end;


                // Text Mail
                if msg.MessageParts.Count =0 then
                begin
                memo1.Lines.Add('TXT-Mail:');
                  memo1.Lines.AddStrings(msg.body);
                  Msg.Body.SaveToFile(ExtractFilePath(Application.ExeName) +'/Mails/text_' + IntToStr(intIndex) + '.txt');
                end;



            memo1.Lines.Append('------------------------------------------');
            memo1.Lines.Append('');
            memo1.Lines.Append('');
            memo1.Lines.Append('');



      end;
      end;
      POP.Disconnect;



end;

end.

CCRDude 28. Jun 2012 11:50

AW: eMails per idPOP3 abholen und als html speichern..
 
Stabil? Das ist keine Ja/Nein-Frage, kommt auf den gewünschten Grad an.
  • Ohne Überprüfung, ob Verbindung überhaupt erfolgreich, bekommt der Benutzer evtl. Fehler gar nicht mit.
  • Ohne Exception-Handling innerhalb der Schleife bedeutet ein Fehler (Dateischreibprobleme, schlecht formatierte Datei, etc.) gleich, dass alle weiteren Mails auch nicht gespeichert werden.
  • Ohne Prüfung des in der Mail angegeben Dateinamens auf Gültigkeit können manche Dateien vllt. nicht geschrieben werden (plus voriges).

Um nur so ein paar "so ginge es stabiler" zu nennen :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:48 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz