These are chat archives for IndySockets/Indy

22nd
Jan 2019
Furma94
@Furma94
Jan 22 15:22

Hi @rlebeau
I Search,study and read all your Online Guide about my problem (Indy comp on Delphi) , but i dont find anything useful for me.
I'm a Delphi Dev , using indy 10.6 on Delphi 10.0.3
Now my problem [Deeper]:
My company have a net full of restrictions , i can't install a .dll on users outlook.exe [V.2016] , i can't intercept imap / pop / smtp / or nothing.
And then , the idea comes. (With Idea , i means my last chance to avoid my net restriction).

procedure TFrmMain.FormCreate(Sender: TObject);
begin
  DragAcceptFiles(Handle, True);
end;

With this Handler i allow my form to receive DragAndDrop Items.msg from Outlook 20016.
Drag From outlook -> To Desktop
From Desktop -> To My Form

procedure TFrmMain.WMDropFiles(var Msg: TMessage);
var
  hDrop: THandle;
  FileCount: Integer;
  NameLen: Integer;
  I: Integer;
  S: string;
begin
  hDrop:= Msg.wParam;
  FileCount:= DragQueryFile (hDrop , $FFFFFFFF, nil, 0);
  for I:= 0 to FileCount - 1 do begin
    NameLen:= DragQueryFile(hDrop, I, nil, 0) + 1;
    SetLength(S, NameLen);
    DragQueryFile(hDrop, I, Pointer(S), NameLen);
  end;
  DragFinish(hDrop);
end;

Now that i've My S variable i open it inTidMessage.LoadFromFile(S,False);

Now , i take the Data i need (Subj / From / Body ) with this :

   Var1:=Trim(TidMessage.From.Text);
    Var2:=DateToStr(TidMessage.Date));
    Var3:=TidMessage.Subject;
    if TidMessage.Priority=mpLowest then Var4:=0;
    if TidMessage.Priority=mpLow then Var4:=1;
    if TidMessage.Priority=mpNormal then Var4:=2;
    if TidMessage.Priority=mpHighthen Var4:=3;
    if TidMessage.Priority=mpHighest then Var4:=4;
   Var5:=TidMessage.MsgId;

BUT everytime i try to take attachment (As i use to do with many of my TiDImap4)

  for i := 0 to Messaggio.MessageParts.Count-1 do
  BEGIN
     CODE
   END

Everytime , messageparts.count = 0
Do you know what am i doing Wrong? Or maybe where i can found the solution or the concept i miss?

P.s.
I'm Really sorry for my english , but it's not my native language and i try my best.

Remy Lebeau
@rlebeau
Jan 22 19:06
@Furma94 in your WM_DROPFILES handler, when calling SetLength() to allocate S, DO NOT use +1 to include the null terminator. a null terminator is already implicitly included in the allocated memory but not counted by the length. You are allocating a 2nd null terminator and not removing it after populating S, so you are actually passing S+#0 to LoadFromFile(), and that can fail since the #0 would get treated as a character in the requested filename but doesn't exist in the actual file on disk. That being said, if you implement the IDropTarget interface in your Form and use RegisterDragDrop() instead of DragAcceptFiles() then you won't even need to drop emails onto your Desktop at all, you can drag the emails from Outlook directly to your Form instead.
@Furma94 Now, aside form that, the only way that the MessageParts would be 0 is if either 1) there really are no attachments in the email, 2) the email is malformed, or 3) the email not being parsed correctly. I'm leaning towards 3 in your case, but I'm only guessing since you didn't show the actual content of the email files you are trying to load.