Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1130 connectés 

  FORUM HardWare.fr
  Programmation
  Delphi/Pascal

  Extraire le fax d'un mail

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Extraire le fax d'un mail

n°1509710
chrisledev​eloppeur
Posté le 01-02-2007 à 17:15:54  profilanswer
 

Bonjour, je réalise une appli qui lorsque l'on y glisse un mail n'importe où dans sa fiche , enregistre ce mail en fichier eml dans un répertoire, ses pièces jointes comprises. le problème reste que je souhaite filtrer les fax reçus par mail vers un traitement spécial pour n'enregistrer que le fichier TIF associé au fax joint dans le mail. Je ne parviens pas à dissocier les fichiers TIF des fichiers joints en général, tous associés au même type de format presse papier. En effet, le cformat retourné est toujours le même nombre, quelquesoit la pièce jointe.  
Voici un code général dont je me suis inspiré pour récupérer le mail et son contenu droppé:

Code :
  1. unit Unit1;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5.   StdCtrls, ComObj, ActiveX, ShlObj;
  6. type
  7.   // Form that acts as IDropTarget
  8.   TForm1         =  class(TForm, IDropTarget)
  9.     procedure    FormCreate(Sender: TObject);
  10.     procedure    FormClose(Sender: TObject; var Action: TCloseAction);
  11.   private
  12.      // Private declarations
  13.      function    SaveAsStg(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
  14.      function    SaveAsStm(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
  15.      function    SaveAsMem(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
  16.   protected
  17.      // Protected declarations
  18.      function    DragEnter(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; stdcall;
  19.      function    DragOver(grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; reintroduce; stdcall;
  20.      function    DragLeave: HResult; stdcall;
  21.      function    Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; stdcall;
  22.   public
  23.      // Public declarations
  24.   end;
  25. // Acceptable format types
  26. const
  27.   CF_Acceptable:          Array [0..1] of String =   ('RenPrivateMessages',
  28.                                                       'Internet Message (rfc822/rfc1522)');
  29. // Predefined clipboard formats
  30. const
  31.   CF_PredfinedFormats:    Array [1..15] of String =  ('Text',
  32.                                                       'Bitmap',
  33.                                                       'MetaFile',
  34.                                                       'SYLK',
  35.                                                       'DIF',
  36.                                                       'TIFF',
  37.                                                       'OemText',
  38.                                                       'DIB',
  39.                                                       'Palette',
  40.                                                       'Pen Data',
  41.                                                       'RIFF',
  42.                                                       'Wave',
  43.                                                       'Unicode Text',
  44.                                                       'Enhanced Metafile',
  45.                                                       'HDrop');
  46. var
  47.   Form1:         TForm1;
  48. implementation
  49. {$R *.DFM}
  50. function TForm1.DragEnter(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult;
  51. var  pEFETC:     IEnumFORMATETC;
  52.      FmtETC:     tagFORMATETC;
  53.      lpszFormat: Array [0..255] of Char;
  54.      szFormat:   String;
  55.      dwAllow:    Integer;
  56.      dwfetch:    Integer;
  57.      bAllow:     Boolean;
  58. begin
  59.   // Clear the format string
  60.   szFormat:='';
  61.   // Determine if we want to support the drop of this type
  62.   bAllow:=False;
  63.   if (dataObj.EnumFormatEtc(DATADIR_GET, pEFETC) = S_OK) then
  64.   begin
  65.      // Start the fetch enumeration
  66.      while (pEFETC.Next(1, FmtETC, @dwfetch) = S_OK) do
  67.      begin
  68.         // Attempt to get the clipboard format name
  69.         if (FmtETC.cfFormat in [1..15]) then
  70.            szFormat:=CF_PredfinedFormats[FmtETC.cfFormat]
  71.         else if (GetClipboardFormatName(FmtETC.cfFormat, lpszFormat, SizeOf(lpszFormat)) > 0) then
  72.            szFormat:=lpszFormat
  73.         else
  74.            szFormat:=IntToStr(FmtETC.cfFormat);
  75.         // Compare formats
  76.         for dwAllow:=0 to High(CF_Acceptable) do
  77.         begin
  78.            if (CompareText(CF_Acceptable[dwAllow], szFormat) = 0) then
  79.            begin
  80.               bAllow:=True;
  81.               if bAllow then break;
  82.            end;
  83.         end;
  84.         if bAllow then break;
  85.      end;
  86.      // Release the enumerator
  87.      pEFETC:=nil;
  88.   end;
  89.   // Should we allow the enter?
  90.   result:=NOERROR;
  91.   if not(bAllow) then dwEffect:=DROPEFFECT_NONE;
  92. end;
  93. function TForm1.DragOver(grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult;
  94. begin
  95.   // Allow the drag over
  96.   result:=NOERROR;
  97. end;
  98. function TForm1.DragLeave: HResult;
  99. begin
  100.   // Allow the drag leave
  101.   result:=NOERROR;
  102. end;
  103. function TForm1.SaveAsStg(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
  104. var  medium:     TStgMedium;
  105.      pvStg:      IStorage;
  106. begin
  107.   // Set default result
  108.   result:=False;
  109.   // Set tymed
  110.   fmt.tymed:=TYMED_ISTORAGE;
  111.   // Get the data
  112.   if (dataObj.GetData(fmt, medium) = S_OK) then
  113.   begin
  114.      // Create storage file
  115.      if (StgCreateDocfile(PWideChar(WideString(FileNameBase+'.msg')),
  116.         STGM_CREATE or STGM_READWRITE or STGM_SHARE_EXCLUSIVE , 0, pvStg) = S_OK) then
  117.      begin
  118.         // Copy from the dropped storage
  119.         result:=(IStorage(medium.stg).CopyTo(0, nil, nil, pvStg) = S_OK);
  120.         // Release the storage interface
  121.         pvStg:=nil;
  122.      end;
  123.      // We are responsible for releasing the storage medium
  124.      IStorage(medium.stg):=nil;
  125.   end;
  126. end;
  127. function TForm1.SaveAsStm(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
  128. var  msStream:   TMemoryStream;
  129.      pvStm:      IStream;
  130.      medium:     TStgMedium;
  131.      stat:       TStatStg;
  132.      dwSize:     Integer;
  133. begin
  134.   // Set default result
  135.   result:=False;
  136.   // Set tymed
  137.   fmt.tymed:=TYMED_ISTREAM;
  138.   // Get the data
  139.   if (dataObj.GetData(fmt, medium) = S_OK) then
  140.   begin
  141.      // Get the stream
  142.      pvStm:=IStream(medium.stm);
  143.      // Stat to get the size
  144.      if (pvStm.Stat(stat, STATFLAG_NONAME) = S_OK) then
  145.      begin
  146.         // Create memory stream for output
  147.         msStream:=TMemoryStream.Create;
  148.         msStream.Size:=stat.cbSize;
  149.         if (IStream(medium.stm).Read(msStream.Memory, stat.cbSize, @dwSize) = S_OK) then
  150.         begin
  151.            msStream.Size:=dwSize;
  152.            msStream.Position:=0;
  153.            msStream.SaveToFile(FileNameBase+'.eml');
  154.            result:=True;
  155.         end;
  156.         msStream.Free;
  157.      end;
  158.      // Release the stream interface
  159.      IStream(medium.stm):=nil;
  160.   end;
  161. end;
  162. function TForm1.SaveAsMem(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
  163. var  msStream:   TMemoryStream;
  164.      medium:     TStgMedium;
  165.      stat:       TStatStg;
  166.      lpMem:      Pointer;
  167.      dwSize:     Integer;
  168. begin
  169.   // Set default result
  170.   result:=False;
  171.   // Set tymed
  172.   fmt.tymed:=TYMED_HGLOBAL;
  173.   // Get the data
  174.   if (dataObj.GetData(fmt, medium) = S_OK) then
  175.   begin
  176.      // Get the memory and lock it
  177.      lpMem:=GlobalLock(medium.hGlobal);
  178.      // Create memory stream for output
  179.      msStream:=TMemoryStream.Create;
  180.      msStream.Write(lpMem^, GlobalSize(medium.hGlobal));
  181.      msStream.SaveToFile(FileNameBase+'.eml');
  182.      result:=True;
  183.      msStream.Free;
  184.      // Unlock and free the memory
  185.      GlobalUnlock(medium.hGlobal);
  186.      GlobalFree(medium.hGlobal);
  187.   end;
  188. end;
  189. function TForm1.Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult;
  190. var  pEFETC:     IEnumFORMATETC;
  191.      FmtETC:     tagFORMATETC;
  192.      msStream:   TMemoryStream;
  193.      cfEml:      Integer;
  194.      cfFile:     Integer;
  195.      pvStg:      IStorage;
  196.      medium:     TStgMedium;
  197.      bHandle:    Boolean;
  198.      dwFetch:    Integer;
  199.      i64Size:    Int64;
  200.      i64Move:    Int64;
  201. begin
  202.   // Set result
  203.   result:=NOERROR;
  204.   // Enumerate until we get are able to save the contents
  205.   bHandle:=False;
  206.   cfFile:=RegisterClipboardFormat(CFSTR_FILECONTENTS);
  207.   cfEml:=RegisterClipboardFormat('Internet Message (rfc822/rfc1522)');
  208.   if (dataObj.EnumFormatEtc(DATADIR_GET, pEFETC) = S_OK) then
  209.   begin
  210.      // Start the fetch enumeration
  211.      while (pEFETC.Next(1, FmtETC, @dwfetch) = S_OK) do
  212.      begin
  213.         // Check file contents
  214.         if (FmtETC.cfFormat = cfFile) then
  215.         begin
  216.            // Try as storage first, then stream
  217.            if ((FmtETC.cfFormat and TYMED_ISTORAGE) = TYMED_ISTORAGE) then
  218.            begin
  219.               if SaveAsStg('Test', dataObj, FmtEtc) then break;
  220.            end;
  221.            if ((FmtETC.cfFormat and TYMED_ISTREAM) = TYMED_ISTREAM) then
  222.            begin
  223.               if SaveAsStm('Test', dataObj, FmtEtc) then break;
  224.            end;
  225.         end
  226.         // Check for eml
  227.         else if (FmtETC.cfFormat = cfEml) then
  228.         begin
  229.            if SaveAsMem('Test', dataObj, FmtEtc) then break;
  230.         end;
  231.      end;
  232.   end;
  233. end;
  234. procedure TForm1.FormCreate(Sender: TObject);
  235. begin
  236.   // Register the form as a drop target
  237.   RegisterDragDrop(Handle, Self);
  238. end;
  239. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  240. begin
  241.   // Revoke the form as a drop target
  242.   RevokeDragDrop(Handle);
  243. end;
  244. initialization
  245.   // Initialize ole
  246.   OleInitialize(nil);
  247. finalization
  248.   // Uninitialize ole
  249.   OleUninitialize;
  250. end.


 
 
 
J'ai eu l'idée également de sauvegarder d'abord tout le mail puis de l'ouvrir en faisant l'extraction du TIF avant sa suppression. Traitement de bidouille je sais. Mais de toute façon, je n'ai pas trouvé le composant standard permettant de lire un fichier EML local (sans passer par un client server de mail). L'idée serait de partir d'un chargement dans un memorystream mais aprés je ne sais pas comment faire pour l'associer à par exemple un MailMessage me permettant de lire le EML. Avez-vous une idée sur soit la solution d'extraire le TIF d'un EML local, soit de directement détecté un TIF en pièce jointe du mail droppé? Merci.

mood
Publicité
Posté le 01-02-2007 à 17:15:54  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Delphi/Pascal

  Extraire le fax d'un mail

 

Sujets relatifs
Utilisation de SQL MailFonction Mail (erreur...)
COMMENT faire pour que mon MAIL ne soit pas considéré comme un SPAM ?proteger un mail de contact sur son site web ?
[Javamail] Comment extraire les PJ de type mail ?fonction mail () Cc et Bc
extraire des mots en fonction de leur mise en forme[Résolu] Envoyer par mail le résultat d'un formulaire fait en html
Question mail() à 2 sous 
Plus de sujets relatifs à : Extraire le fax d'un mail


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR