Re: IdSMTPServer.OnMsgReceive event: AMsg TStream is always blank

Giganews Newsgroups
Subject: Re: IdSMTPServer.OnMsgReceive event: AMsg TStream is always blank
Posted by:  Remy Lebeau (Indy Team) (no.spam@no.spam.com)
Date: Wed, 4 Apr 2007

"mysticforces" <paul.four…@malans.co.za> wrote in message
news:B8F523540E21E340paul.four…@malans.co.za...

> I'm using version 10.1.5

The current version is 10.1.6.

>    lsStr.LoadFromStream(AMsg);

Did you verify that AMsg.Position is 0 before calling
LoadFromStream()?

>    IdMessage1.Recipients.Assign(ASender.RCPTList);

Keep in mind that the RCPTList contains all of the 'To', 'Cc', and
'Bcc' recipients in a single list.  If your client uses those header
('Bcc' in particular), then the recpients will be able to see all of
the other recipients, which may not be desirable.

>    IdMessage1.Body.AddStrings(lsStr);

The AMsg stream contains the complete message, including all of the
original headers.  You are including those headers in the body of the
message you are sending.  You need to strip them out.  The most
accurate thing you can do to preserve the original message is to send
the AMsg stream as-is without using TIdMessage at all.  Of course,
that would mean issue SMTP commands manually as well, but that is not
very hard.  For example:

    type
        TIdSMTPAccess = class(TIdSMTP);

    procedure TForm1.IdSMTPServer1MsgReceive(ASender:
TIdSMTPServerContext; AMsg: TStream; var LAction: TIdDataReply);
    var
        lsStr: TStringList;
    begin
        lsStr := TStringList.Create;
        try
            AMsg.Position := 0;
            lsStr.LoadFromStream(AMsg);
            IdSMTP1.Connect;
            try
                IdSMTP1.SendCmd(MAILFROM_CMD + '<' + ASender.From +
'>', MAILFROM_ACCEPT);
                try

TIdSMTPAccess(IdSMTP1).WriteRecipientsNoPipelining(ASender.RCPTList);
                    IdSMTP1.SendCmd(DATA_CMD, DATA_ACCEPT);
                    IdSMTP1.IOHandler.WriteRFCStrings(lsStr, False);
                    IdSMTP1.SendCmd('.', DATA_PERIOD_ACCEPT);
                except
                    on E: EIdSMTPReplyError do begin
                        IdSMTP1.SendCmd(RSET_CMD);
                        raise;
                    end;
                end;
            finally
                IdSMTP1.Disconnect;
            end;
        finally
            lsStr.Free;
        end;
        LAction := dOk;
    end;

> My point is, the AMsg variable passed through by the event doesn't
> have the 'message' I'm expecting.

Yes, it does.  It contains the raw message data as provided by the
client.

> The TStream is totally blank when I evaluated it.  Size = 0.

AMsg is guaranteed to never be empty.  Do keep in mind, however, that
the IDE debugger might be displaying a Size of 0 when the Size is not
actually 0.  That has been known to happen before.

Gambit

Replies

In response to

IdSMTPServer.OnMsgReceive event: AMsg TStream is always blank posted by mysticforces on Wed, 4 Apr 2007