Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
nightfreak  
#1 Posted : Wednesday, February 15, 2017 4:13:06 AM(UTC)
nightfreak

Rank: Newbie

Groups: Registered
Joined: 2/15/2017(UTC)
Posts: 2
Austria

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  
#2 Posted : Wednesday, February 15, 2017 5:03:26 PM(UTC)
ivan

Rank: Administration

Groups: Administrators
Joined: 11/11/2010(UTC)
Posts: 1,148

Thanks: 9 times
Was thanked: 54 time(s) in 54 post(s)
Hi, you can use AddAttachment1 method:
https://www.emailarchite...l/sdk/?ct=addattachment1


Here is the example codes to convert MemoryStream to VARIANT, you need to read file to memory stream, then use AddAttachment1
Code:
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)
Code:

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;

Edited by user Friday, December 11, 2020 5:11:06 PM(UTC)  | Reason: Not specified

nightfreak  
#3 Posted : Thursday, February 16, 2017 9:50:06 AM(UTC)
nightfreak

Rank: Newbie

Groups: Registered
Joined: 2/15/2017(UTC)
Posts: 2
Austria

Thank you very much!!
This works for me :-)

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.145 seconds.

EXPLORE TUTORIALS

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