🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #4701

Re: Indy 8.0 TIdSmptServer

#4701
From: Remy Lebeau (TeamB)
Date: Tue, 04 Jan 2005 20:58
116 lines
5013 bytes
"Frans" <invalid@wanadoo.nl> wrote in message
news:41dae5ae@newsgroups.borland.com...

> Only i still don't understand the way to filter out the attachments.

Loop through the MessageParts collection looking for TIdAttachment
instances, ie:

    void __fastcall TForm1::IdSMTPServer1ReceiveMessageParsed(TIdCommand
*ASender, TIdMessage *&AMsg, TIdEMailAddressList *RCPT, AnsiString
&CustomError)
    {
        //...
        for(int x = 0; x < AMsg->MessageParts->Count; ++x)
        {
            TIdMessagePart *Part = AMsg->MessageParts->Items[x];
            if( TIdAttachment *attach = dynamic_cast<TIdAttachment*>(Part) )
                attach->SaveToFile(SomeFileNameHere);
        }
        //...
    }

> Also the body show's the prefix text / preamble instead of the
> the body text.

That is correct.  The message has been MIME encoded.  The actual message
text has been parsed out and stored in the MessageParts collection, this
time in TIdText instances, ie:

    void __fastcall TForm1::IdSMTPServer1ReceiveMessageParsed(TIdCommand
*ASender, TIdMessage *&AMsg, TIdEMailAddressList *RCPT, AnsiString
&CustomError)
    {
        //...
        if( AMsg->MessageParts->Count > 0 )
        {
            for(int x = AMsg->MessageParts->Count-1; x >= 0; --x)
            {
                TIdMessagePart *Part = AMsg->MessageParts->Items[x];
                if( TIdText *txt = dynamic_cast<TIdAttachment*>(Part) )
                {
                    if( txt->ContentType is a format that you support )
                    {
                        Memo->Lines = txt->Body;
                        break;
                    }
                }
            }
        }
        else
            Memo->Lines = AMsg->Body;
        //...
    }

Now, with that said, accessing the TMemo directly like shown above is very
dangerous (as is calling ShowMessage()) because the event handler is
triggered in the context of a worker thread, not the main VCL thread.  As
such, you must use the thread's Synchronize() method to access the main VCL
thread in a safe manner. I suggest using TIdSync for that, ie:

    class TUpdateMemo : public TIdSync
    {
    protected:
        TStrings *FStrings;
        void __fastcall DoSynchronize() {
Form1->Memo1->Lines->Assign(FStrings); }
    public:
        __fastcall TUpdateMemo(AThread: TIdThread, TStrings *AStrings)
            : TIdSync(AThread), FStrings(AStrings) {}
    };

    void __fastcall TForm1::IdSMTPServer1ReceiveMessageParsed(TIdCommand
*ASender, TIdMessage *&AMsg, TIdEMailAddressList *RCPT, AnsiString
&CustomError)
    {
        //...
        if( AMsg->MessageParts->Count > 0 )
        {
            for(int x = AMsg->MessageParts->Count-1; x >= 0; --x)
            {
                TIdMessagePart *Part = AMsg->MessageParts->Items[x];
                if( TIdText *txt = dynamic_cast<TIdAttachment*>(Part) )
                {
                    // looping backwards, you should stop when you
                    // detect the most complex format that you want to
support,
                    // such as "text/html", "text/rtf", etc...
                    if( txt->ContentType == "text/plain" )
                    {
                        TUpdateMemo(ASender->Thread,
txt->Body).Synchronize();
                        break;
                    }
                }
            }
        }
        else
            TUpdateMemo(ASender->Thread, AMsg->Body).Synchronize();
        //...
    }

> I know i'm doing something wrong but i have now idea what !

You must not have googled very hard, because this has been discussed many
times before.

> also the attachment count show's 0 instead of the correct number
> of attachments

As well it should.  The AttachmentCount (and RelatedPartCount and
TextPartCount) is updated when TIdMessage::CountParts() is called, when is
only done when sending a message, not receiving it.


Gambit
Technical Details
Message-ID: <41db0330$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <41d950fd@newsgroups.borland.com> <41d98fde$1@newsgroups.borland.com> <41d9aeb2$1@newsgroups.borland.com> <41d9b514@newsgroups.borland.com> <41dae5ae@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,7edc2c9797771d27
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <41d950fd@newsgroups.borland.com> <41d98fde$1@newsgroups.borland.com> <41d9aeb2$1@newsgroups.borland.com> <41d9b514@newsgroups.borland.com> <41dae5ae@newsgroups.borland.com>
Subject: Re: Indy 8.0 TIdSmptServer
Date: Tue, 4 Jan 2005 12:58:54 -0800
Lines: 118
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <41db0330$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1104872240 68.69.17.41 (4 Jan 2005 12:57:20 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail