nightfreak
8 years ago
Hello,
in Delphi, I want to send a file as attachment which is stored in a TFileStream.
Does anyone know how to do this and could post the code for it?
I could only find examples for sending a file which is on my harddisk or a http source.

Thanks,
​​​​​​​Chris

ivan
  • ivan
  • 100% (Exalted)
  • Administration
8 years ago
Hi, you can use AddAttachment1 method:
https://www.emailarchitect.net/easendmail/sdk/?ct=addattachment1 


Here is the example codes to convert MemoryStream to VARIANT, you need to read file to memory stream, then use AddAttachment1
function MemoryStreamToOleVariant(Strm: TMemoryStream): OleVariant;
var
  Data: PByteArray;
begin
 Result := VarArrayCreate([0, Strm.Size - 1], varByte);
 Data := VarArrayLock(Result);
 
 try

   Strm.Position := 0;
   Strm.ReadBuffer(Data^, Strm.Size);

 finally

   VarArrayUnlock(Result);

 end;
end;

Additional example codes (OleVariant to TMemoryStream)

function OleVariantToMemoryStream(data: OleVariant): TMemoryStream;
var
  ms: TMemoryStream;
  i, len, lbound, ubound: integer;
  buffer: array of byte;
begin
  result := TMemoryStream.Create;

  lbound := VarArrayLowBound(data, 1);
  ubound := VarArrayHighBound(data, 1);
  len := ubound - lbound + 1;

  if len = 0 then
    exit;

  SetLength(buffer, len);
  for i := lbound to ubound do
  begin
    buffer[i] := VarArrayGet(data, i);
  end;

  result.WriteBuffer(buffer[0], len);
  result.Seek(0, soFromBeginning);

end;
nightfreak
8 years ago
Thank you very much!!
This works for me :-)

EXPLORE TUTORIALS

© All Rights Reserved, AIFEI Software Limited & AdminSystem Software Limited.