Save outlook attachments to folder automatically lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Stygian · 2017-02-08 12:27 Hello, I would like to save some outlook attachments into a specified folder. What is the best method to do this? Thanks, Sty ---------------------------------------------------------------------- Thaddy · 2017-02-08 12:43 Don't. Saving without user consent is a security risk. Such a question raises my aluhat.... Not a good idea. If you need that for your own use: OK, but never implement that in clients that other people use. It smells like criminal intend. I know you don't mean it that way, but this is why major clients don't support that. ---------------------------------------------------------------------- Stygian · 2017-02-08 12:48 Dear Thaddy, This attachment is sent by me to another user and contain safe datas. It's a must because this is the only way to exchange these information. Regards, Sty ---------------------------------------------------------------------- wp · 2017-02-08 13:25 Back in my Delphi days, I once bought the component smmsg from Scalabium Software (http://www.scalabium.com/), no problem to extract attachments from my Outlook pst and msg files even without having Outlook installed. It comes with sources, but I never tried if it works in Lazarus, it certainly is very much Windows-loaded. Without this library you must have Outlook on your computer and use the provided OLE interface which probably gives access to the attachments as well. ---------------------------------------------------------------------- Stygian · 2017-02-08 13:51 Dear wp, I have Outlook installed on the PC and i also found a code on the internet but it's for Delphi. I have problem with the "namespace.logon" Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure readmails;var NumMails: integer; outlook: OLEvariant; namespace: OLEvariant; folder: OLEvariant; MailCounter: integer; MailMessage: variant; NUMATTACHMENTS: integer; counter: integer; ATTFILENAME : variant; FILENAME : string;begin NumMails:=0; namespace.logon(EmptyPar am,EmptyParam,False,True);folder:=namespace.getdefaultfolder(6); MailC ounter:=1; PROCESSINGlbl.caption:='Processing 0 of '+inttostr(folder.items.count)+' E-mails'; while (folder.items.count>0) do begin  NumMails:=NumMails+1;  MailCounter:=folder.items.count;  MailMessage:=folder.items(MailCounter);  PROCESSINGlbl.caption:='Processing '+inttostr(MailCounter)+' of '+inttostr(folder.items.count)+' E-mails';   NUMATTACHMENTS:=MailMessage.attachments.count;  FOR counter:=1 to NUMATTACHMENTS DO   BEGIN    ATTFILENAME:=MailMessage.Attachments.item(counter).filename;    filename:='C:\ATTACHMENTS\'+ATTFILENAME;    MailMessage.attachments.item(counter).saveasfile(filename);   END;   MailMessage.delete; end; end; Does Lazarus support namespace? Regards, Sty. ---------------------------------------------------------------------- wp · 2017-02-08 14:33 You should look more carefully at the source where this code snippet is from. I am sure that "namespace" is created somewhere, Maybe like this which is an adaption of https://msdn.microsoft.com/en- us/library/office/ff861594.aspx: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  olApp: OLEVariant;  namespace: OLEVariant;...  olApp := CreateObject('Outlook.Application');  namespace := olApp.GetNamespace('MAPI'); In the meantime I was able to convert smmsg to Lazarus, the package compiles and the demo runs and opens a few provided msg files (some few issues left probably due to strings). Almost no effort, amazing! Unfortunately this is not free software and I cannot upload it here. But it's good to know, at least. ---------------------------------------------------------------------- Stygian · 2017-02-10 10:51 Thank You wp, I scapped that code and rewrite it from MSDN. Here is the final result if someone interested Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Save_Mail;var  Outlook : OLEVariant;  namespace : OLEVariant;  folder : OLEVariant;  atcments, attfilename : Variant;  NumOfMails : integer;  MMessages : Variant;  NumOfAttach, counter : integer;begin NumOfMails := 0;  try  Outlook := GetActiveOleObject('Outlook.Application');  except  Outlook := CreateOleObject('Outlook.Application');  end; namespace := Outlook.GetNamespace('MAPI'); folder := namespace.GetDefaultFolder(6); //6 means Inbox. You can find the list on MSDN NumOfMails := folder.items.count; MMessages := folder.items(NumOfMails); atcments := MMessages.attachments.count;  for counter := 1 to atcments do   begin   attfilename := MMessages.Attachments.item(counter).filename;    if(attfilename = 'filename.extension')then //i just need a specify file. Delete it if not needed     begin     MMessages.attachments.item (counter).saveasfile('yourfolder'+attfilename);     end;   end; MMessages.delete; //This will delete the message. Comment out if you want to leave it in your inbox Outlook := Unassigned;end; Regards, Sty.