* * *

Author Topic: Save outlook attachments to folder automatically  (Read 839 times)

Stygian

  • Jr. Member
  • **
  • Posts: 86
Save outlook attachments to folder automatically
« on: February 08, 2017, 01:27:45 pm »
Hello,

I would like to save some outlook attachments into a specified folder. What is the best method to do this?

Thanks,
Sty

Thaddy

  • Hero Member
  • *****
  • Posts: 3429
Re: Save outlook attachments to folder automatically
« Reply #1 on: February 08, 2017, 01:43:37 pm »
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.
« Last Edit: February 08, 2017, 01:47:27 pm by Thaddy »

Stygian

  • Jr. Member
  • **
  • Posts: 86
Re: Save outlook attachments to folder automatically
« Reply #2 on: February 08, 2017, 01:48:28 pm »
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

  • Hero Member
  • *****
  • Posts: 3450
Re: Save outlook attachments to folder automatically
« Reply #3 on: February 08, 2017, 02:25:54 pm »
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.
Lazarus trunk / fpc 3.0.0 / Win32

Stygian

  • Jr. Member
  • **
  • Posts: 86
Re: Save outlook attachments to folder automatically
« Reply #4 on: February 08, 2017, 02:51:36 pm »
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]
  1. procedure readmails;
  2. var
  3.  NumMails: integer;
  4.  outlook: OLEvariant;
  5.  namespace: OLEvariant;
  6.  folder: OLEvariant;
  7.  MailCounter: integer;
  8.  MailMessage: variant;
  9.  NUMATTACHMENTS: integer;
  10.  counter: integer;
  11.  ATTFILENAME : variant;
  12.  FILENAME : string;
  13. begin
  14.  
  15. NumMails:=0;
  16.  
  17. namespace.logon(EmptyParam,EmptyParam,False,True);
  18. folder:=namespace.getdefaultfolder(6);
  19.  
  20. MailCounter:=1;
  21.  
  22. PROCESSINGlbl.caption:='Processing 0 of '+inttostr(folder.items.count)+' E-mails';
  23.  
  24. while (folder.items.count>0) do
  25.  begin
  26.   NumMails:=NumMails+1;
  27.   MailCounter:=folder.items.count;
  28.   MailMessage:=folder.items(MailCounter);
  29.   PROCESSINGlbl.caption:='Processing '+inttostr(MailCounter)+' of '+inttostr(folder.items.count)+' E-mails';
  30.  
  31.   NUMATTACHMENTS:=MailMessage.attachments.count;
  32.   FOR counter:=1 to NUMATTACHMENTS DO
  33.    BEGIN
  34.     ATTFILENAME:=MailMessage.Attachments.item(counter).filename;
  35.     filename:='C:\ATTACHMENTS\'+ATTFILENAME;
  36.     MailMessage.attachments.item(counter).saveasfile(filename);
  37.    END;
  38.  
  39.   MailMessage.delete;
  40.  end;
  41.  
  42. end;

Does Lazarus support namespace?

Regards,
Sty.

wp

  • Hero Member
  • *****
  • Posts: 3450
Re: Save outlook attachments to folder automatically
« Reply #5 on: February 08, 2017, 03:33:50 pm »
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]
  1. var
  2.   olApp: OLEVariant;
  3.   namespace: OLEVariant;
  4. ...
  5.   olApp := CreateObject('Outlook.Application');
  6.   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.
Lazarus trunk / fpc 3.0.0 / Win32

Stygian

  • Jr. Member
  • **
  • Posts: 86
Re: Save outlook attachments to folder automatically
« Reply #6 on: February 10, 2017, 11:51:52 am »
Thank You wp,

I scapped that code and rewrite it from MSDN.

Here is the final result if someone interested
Code: Pascal  [Select]
  1. procedure TForm1.Save_Mail;
  2. var
  3.   Outlook : OLEVariant;
  4.   namespace : OLEVariant;
  5.   folder : OLEVariant;
  6.   atcments, attfilename : Variant;
  7.   NumOfMails : integer;
  8.   MMessages : Variant;
  9.   NumOfAttach, counter : integer;
  10. begin
  11.  NumOfMails := 0;
  12.   try
  13.   Outlook := GetActiveOleObject('Outlook.Application');
  14.   except
  15.   Outlook := CreateOleObject('Outlook.Application');
  16.   end;
  17.  namespace := Outlook.GetNamespace('MAPI');
  18.  folder := namespace.GetDefaultFolder(6); //6 means Inbox. You can find the list on MSDN
  19.  NumOfMails := folder.items.count;
  20.  MMessages := folder.items(NumOfMails);
  21.  atcments := MMessages.attachments.count;
  22.   for counter := 1 to atcments do
  23.    begin
  24.    attfilename := MMessages.Attachments.item(counter).filename;
  25.     if(attfilename = 'filename.extension')then //i just need a specify file. Delete it if not needed
  26.      begin
  27.      MMessages.attachments.item(counter).saveasfile('yourfolder'+attfilename);
  28.      end;
  29.    end;
  30.  MMessages.delete; //This will delete the message. Comment out if you want to leave it in your inbox
  31.  Outlook := Unassigned;
  32. end;

Regards,
Sty.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus