Re: TidMessage Plan / HTML with Images and Attachment LoadFrom / SaveTo STREAM - USING INDY10

Giganews Newsgroups
Subject: Re: TidMessage Plan / HTML with Images and Attachment LoadFrom / SaveTo STREAM - USING INDY10
Posted by:  Ant (adcru…@tickets_NOSPAM.com)
Date: Tue, 15 May 2007

Isee other peole with the sam problem but no solution.
AFAIK, I am using the latest Indy 10.

So I have decided to rebuild the message as below

function TForm1.RebuildMsg( OldMsg : TidMessage; Attachment :
TMemoryStream ) : TidMessage;
var
  i : integer;
  atc : TIdAttachmentMemory;
begin
  //OK - we know this has an attwechment as they are the ones that cause a
problem!!!!
  //Remember that the attachment is missing and we are adding it back in
from the stream
  //Hence..............
  //PART 0 Empty - ParentPart -1
  //PART 1 Plain Text - ParentPart 0
  //PART 2 Empty - ParentPart 0
  //PART 3 HTML version - ParentPart 2
  //PART 4, 5 and so on are the embedded images ParentPart 2
  //LAST PART is tye attachment ParentPart -1

  result := TIdMessage.Create;
  With Result do
  begin
      Subject                  := 'TEST REBUILD';
      From.Name                := 'dev';
      From.Address              := 'ripte…@notimeforrepair.com';
      Recipients.EMailAddresses := 'e…@eggy.com';
      ConvertPreamble := False;
      ContentType := 'multipart/mixed';
      Encoding := meMIME;
  end;

  //First lets do Parts 0 to 3 as they will be the same
  With TIdText.Create( Result.MessageParts ) do
      begin
      //!!!  PART 0  !!!
      Body.Text  := '';
      ContentType := 'multipart/alternative';
      ParentPart  := -1;
      end;

  With TIdText.Create( Result.MessageParts ) do
      begin
      //!!!  PART 1  !!!
      Body.Text  := TidText( OldMsg.MessageParts[ 1 ] ).Body.Text;
      ContentType := TidText( OldMsg.MessageParts[ 1 ] ).ContentType;
      ParentPart  := -1;
      ContentTransfer := '7bit';
      ParentPart  := 0;
      end;

  With TIdText.Create( Result.MessageParts ) do
      begin
      //!!!  PART 2  !!!
      Body.Text  := '';
      ContentType := 'multipart/related';
      ParentPart  := 0;
      end;

  With TIdText.Create( Result.MessageParts ) do
      begin
      //!!!  PART 3  !!!
      Body.Text  := TidText( OldMsg.MessageParts[ 3 ] ).Body.Text;
      ContentTransfer := 'quoted-printable';
      ContentType := 'text/html';
      ParentPart := 2
      end;

  //Now X number of emnbeddd image attachments
  for i := 4 to OldMsg.MessageParts.Count - 1 do
  with TidAttachmentMemory.Create( Result.MessageParts ) do
      begin
      //!!!  PART 4  !!!
      TIdAttachmentFile( OldMsg.MessageParts[ i ] ).SaveToStream(
DataStream );
      ContentTransfer    := TIdAttachmentFile( OldMsg.MessageParts[
i ] ).ContentTransfer;
      ParentPart        := 2;
      DisplayName        := TIdAttachmentFile( OldMsg.MessageParts[
i ] ).DisplayName;
      FileName          := TIdAttachmentFile( OldMsg.MessageParts[
i ] ).FileName;
      ContentType        := TIdAttachmentFile( OldMsg.MessageParts[
i ] ).ContentType;
      ContentID          := TIdAttachmentFile( OldMsg.MessageParts[
i ] ).ContentID;
      ContentDisposition := 'inline';
      end;//with

  //And finally - the actual ticket attachment
  Attachment.Seek( 0, soBeginning );
  atc := TidAttachmentMemory.Create( Result.MessageParts, Attachment );
  atc.ContentType := 'application/pdf';
  atc.ContentDisposition := 'attachment';
  atc.FileName := 'Tickets.pdf';
  atc.DisplayName := 'Tickets.pdf';
  atc.ParentPart := -1;

end;

"Ant" <adcruze@tickets_NOSPAM.com> wrote in message
news:CF507A6A1726E340adcruze@tickets_NOSPAM.com...
> Hi all,
> it seems to me that using LoadFromStream SaveToStream with a TidMessage
> screws up the email aa bit.
>
> I ave messages to a VarBinary field in SQL server but when I retrieve them
> and try to send, I fin that the HTML is not shown in outlook.
>
> My message is OK because if I just send it without the database stuff, it
> all comes in fine.
>
> What could be my trouble?
>
> This code is causing me trouble
>
> if MessageDlg( 'Save and Load From DB' , mtConfirmation, [mbYes, mbNo],
> 0 ) = mrYes then
>  begin
>  if ADOQuery1.Active = false then
>      ADOQuery1.Active := true;
>
>  ADOQuery1.Insert;
>  StrM := TMemoryStream.Create;
>  Msg.SaveToStream( StrM );
>  StrM.Seek( 0, soFrombeginning );
>  ( ADOQuery1.FieldByName( 'Message' ) as TBlobField ).LoadFromStream(
> StrM );
>  ADOQuery1.FieldByName( 'EntryDate' ).Value := Now;
>  ADOQuery1.Post;
>
>  msg.Free;
>  StrM.Free;
>
>  StrM := TMemoryStream.create;
>  ( ADOQuery1.FieldByName( 'Message' ) as TBlobField ).SaveToStream(
> StrM );
>  StrM.Seek( 0, soFromBeginning );
>  msg := TIdMessage.Create;;
>  StrM.Seek( 0, soFromBeginning );
>
>  msg.LoadFromStream( StrM, false );
>  end;

Replies

None

In response to

TidMessage Plan / HTML with Images and Attachment LoadFrom / SaveTo STREAM - USING INDY10 posted by Ant on Mon, 14 May 2007