Einzelnen Beitrag anzeigen

Benutzerbild von erich.wanker
erich.wanker

Registriert seit: 31. Jan 2008
Ort: im schönen Salzburger Land
454 Beiträge
 
Delphi XE4 Professional
 
#1

eMails per idPOP3 abholen und als html speichern..

  Alt 28. Jun 2012, 09:53
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.
Erich Wanker - for life:=1971 to lebensende do begin ..
O
/H\
/ \

Geändert von erich.wanker (28. Jun 2012 um 10:34 Uhr)
  Mit Zitat antworten Zitat