IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Web & réseau Delphi Discussion :

[Indy10] [TIdMessage] Pièce jointe envoyée mais invisible


Sujet :

Web & réseau Delphi

  1. #1
    Expert confirmé

    Avatar de sjrd
    Homme Profil pro
    Directeur de projet
    Inscrit en
    Juin 2004
    Messages
    4 517
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Suisse

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2004
    Messages : 4 517
    Par défaut [Indy10] [TIdMessage] Pièce jointe envoyée mais invisible
    Bonjour,

    J'ai un problème particulièrement étrange Je dois envoyer un e-mail avec une version texte, une version HTML et une pièce jointe (une image PNG).

    Sans la pièce jointe, je n'ai aucun problème. Mais voilà ce qui se passe quand j'envoie ma pièce jointe avec :
    Le fichier est bien envoyé (le temps d'envoi est plus long et si je regarde le source du mail reçu il y est bien), mais il est invisible et inaccessible.

    Je m'explique : Outlook ne me signale pas qu'il y a des pièces jointes, donc je ne sais pas y accéder. Et ma messagerie Gmail signale qu'un fichier joint existe (et me montre son nom correct) mais ne me donne aucun lien pour le télécharger !!!

    Je me suis inspiré de la FAQ Delphi pour écrire mon code, et l'ai un peu modifié. Voici ce que ça donne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    procedure TSendThread.Execute;
    var SMTP : TIdSMTP;
        Mail : TIdMessage;
        I : integer;
        Recipient : string;
    begin
      Mail := TIdMessage.Create(nil);
      try
        Mail.ContentType := 'multipart/alternative';
        Mail.Recipients.Add;
        Mail.From.Name := AddressFrom+' '+NameFrom;
        Mail.Subject := Subject;
     
        TIdText.Create(Mail.MessageParts, BodyText).ContentType := 'text/plain';
        TIdText.Create(Mail.MessageParts, Body).ContentType := 'text/html';
        if ImageFileName <> '' then
          TIdAttachmentFile.Create(Mail.MessageParts, ImageFileName).ContentType := 'image/png';
        Mail.MessageParts.CountParts;
     
        SMTP := TIdSMTP.Create(nil);
        try
          SMTP.Host := 'out.versateladsl.be';
          SMTP.Connect;
     
          for I := 0 to Recipients.Count-1 do
          try
            Recipient := Recipients[I];
            Mail.Recipients[0].Name := GetFirstToken(Recipient, #9);
            Mail.Recipients[0].Address := GetLastToken(Recipient, #9);
     
            SMTP.Send(Mail);
          except
          end;
        finally
          SMTP.Free;
        end;
      finally
        Mail.Free;
      end;
    end;
    Les variables AddressFrom, NameFrom, Subject, BodyText, Body, ImageFileName et Recipients sont des variables de la classe TSendThread et sont correctement renseignées à ce moment (vérifié au débogage).

    Voilà j'espère que quelqu'un saura m'aider.
    d'avance
    sjrd, ancien rédacteur/modérateur Delphi.
    Auteur de Scala.js, le compilateur de Scala vers JavaScript, et directeur technique du Scala Center à l'EPFL.
    Découvrez Mes tutoriels.

  2. #2
    Membre émérite
    Avatar de neilbgr
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Août 2004
    Messages
    651
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2004
    Messages : 651
    Par défaut
    Il faut d'abord savoir et comprendre comment fonctionne le "multi-part" d'un e-mail.

    Explications :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    - In Indy 10, you do the same things, except that you use TIdAttachmentFile
    or TIdAttachmentMemory instead of TIdAttachment, and there are extra steps
    involved to set up the TIdText and TIdAttachmentFile/Memory instances
    properly.  Every message part has a ParentPart property that is set to -1 by
    default.  This is an index to the part that is the "parent" of the current
    part.  This is for nesting parts underneath each other. -1 means that a part
    is not nested, but is at the top-level of the message.  Because of this
    nesting support, you end up with a variety of different setups depending on
    what exactly you want to send, as outlined below:
     
    When including just HTML and no plain-text or attachments, put the HTML in
    the TIdMessage.Body and set the TIdMessage.ContentType to 'text/html'.
     
    When including just plain-text and HTML parts and no attachments, leave the
    ParentPart properties set to -1, and set the TIdMessage.ContentType to
    'multipart/alternative'.  You end up with the following TIdMessage layout:
     
        TIdMessage (multipart/alternative)
        {
            TIdText (text/plain), ParentPart -1
            TIdText (text/html), ParentPart -1
        }
     
    When including just HTML and non-related attachments, leave the ParentPart
    properties set to -1, and set the TIdMessage.ContentType to
    'multipart/mixed'.  You end up with the following TIdMessage layout:
     
        TIdMessage (multipart/mixed)
        {
            TIdText (text/html), ParentPart -1
            TIdAttachment (whatever), ParentPart -1
        }
     
    When including just HTML and related attachments, leave the ParentPart
    properties set to -1, and set the TIdMessage.ContentType to
    'multipart/related'.  You end up with the following TIdMessage layout:
     
        TIdMessage (multipart/related)
        {
            TIdText (text/html), ParentPart -1
            TIdAttachment (image/*) ParentPart -1
        }
     
    When including both plain-text and HTML and attachments, the layout depends
    on whether you are including HTML-related attachments or not:
     
    If you are including just related attachments, then you leave the ParentPart
    for the 'text/plain' part set to -1, then you need an extra blank TIdText
    with its ContentType set to 'multipart/related' and the ParentPart for the
    'text/html' part and attachments set to the index of of the
    'multipart/related' part, and then set the TIdMessage.ContentType to
    'multipart/alternative'.  You end up with the following TIdMessage layout:
     
        TIdMessage (multipart/alternative)
        {
            TIdText (text/plain), ParentPart -1
            TIdText (multipart/related), ParentPart -1
            {
                TIdText (text/html), ParentPart 1
                TIdAttachment (image/*) ParentPart 1
            }
        }
     
    If you are including just non-related attachments, then you need an extra
    blank TIdText with its ContentType set to 'multipart/alternative' and the
    ParentPart for both the 'text/plain' and 'text/html' parts set to the index
    of of the 'multipart/altrnative' part, the ParentPart for the attachments
    left at -1, and then set the TIdMessage.ContentType to 'multipart/mixed'.
    You end up with the following TIdMessage layout:
     
        TIdMessage (multipart/mixed)
        {
            TIdText (multipart/alternative), ParentPart -1
            {
                TIdText (text/plain), ParentPart 0
                TIdText (text/html), ParentPart 0
            }
            TIdAttachment (whatever) ParentPart -1
        }
     
    If you are including both related and non-related attachments, then you
    combine the two layous above.  The TIdMessage.ContentType should be set to
    'multipart/mixed'.  You end up with the following TIdMessage layout:
     
        TIdMessage (multipart/mixed)
        {
            TIdText (multipart/alternative), ParentPart -1
            {
                TIdText (text/plain), ParentPart 0
                TIdText (multipart/related), ParentPart 0
                {
                    TIdText (text/html), ParentPart 2
                    TIdAttachment (image/*) ParentPart 2
                }
            }
            TIdAttachment (whatever) ParentPart -1
        }
     
    Gambit
    Extrait du post de Remy Lebeau (TeamB)

  3. #3
    Expert confirmé

    Avatar de sjrd
    Homme Profil pro
    Directeur de projet
    Inscrit en
    Juin 2004
    Messages
    4 517
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Suisse

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2004
    Messages : 4 517
    Par défaut
    Super C'est donc apparemment l'avant-dernier schéma que je dois utiliser.

    Je teste ça de suite et je vous tiens au courant
    sjrd, ancien rédacteur/modérateur Delphi.
    Auteur de Scala.js, le compilateur de Scala vers JavaScript, et directeur technique du Scala Center à l'EPFL.
    Découvrez Mes tutoriels.

  4. #4
    Expert confirmé

    Avatar de sjrd
    Homme Profil pro
    Directeur de projet
    Inscrit en
    Juin 2004
    Messages
    4 517
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Suisse

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2004
    Messages : 4 517
    Par défaut Re: [Indy10] [TIdMessage] Pièce jointe envoyée mais invisibl
    Merveilleux Ca fonctionne parfaitement

    Pour ceux que ça intéresse, voici mon code maintenant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    procedure TSendThread.Execute;
    var SMTP : TIdSMTP;
        Mail : TIdMessage;
        I : integer;
        Recipient : string;
    begin
      Mail := TIdMessage.Create(nil);
      try
        Mail.ContentType := 'multipart/mixed';
        Mail.Recipients.Add;
        Mail.From.Name := AddressFrom+' '+NameFrom;
        Mail.Subject := Subject;
     
        TIdText.Create(Mail.MessageParts).ContentType := 'multipart/alternative';
        with TIdText.Create(Mail.MessageParts, BodyText) do
        begin
          ContentType := 'text/plain';
          ParentPart := 0;
        end;
        with TIdText.Create(Mail.MessageParts, Body) do
        begin
          ContentType := 'text/html';
          ParentPart := 0;
        end;
        if ImageFileName <> '' then
          TIdAttachmentFile.Create(Mail.MessageParts, ImageFileName).ContentType := 'image/png';
        Mail.MessageParts.CountParts;
     
        SMTP := TIdSMTP.Create(nil);
        try
          SMTP.Host := 'out.versateladsl.be';
          SMTP.Connect;
     
          for I := 0 to Recipients.Count-1 do
          try
            Recipient := Recipients[I];
            Mail.Recipients[0].Name := GetFirstToken(Recipient, #9);
            Mail.Recipients[0].Address := GetLastToken(Recipient, #9);
     
            SMTP.Send(Mail);
          except
          end;
        finally
          SMTP.Free;
        end;
      finally
        Mail.Free;
      end;
    end;
    beaucoup neilbgr
    sjrd, ancien rédacteur/modérateur Delphi.
    Auteur de Scala.js, le compilateur de Scala vers JavaScript, et directeur technique du Scala Center à l'EPFL.
    Découvrez Mes tutoriels.

  5. #5
    Membre émérite
    Avatar de neilbgr
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Août 2004
    Messages
    651
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2004
    Messages : 651
    Par défaut
    padcoa

  6. #6
    Membre régulier

    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 9
    Par défaut Chez moi, pas besoin de MULTIPART !
    J'ai développé une appli qui scrute les sous dossiers d'un dossier donné, par défaut "c:\expedition". Dès qu'il y détecte un fichier autre que "email.txt", il expédie un mel avec en pièce jointe le ou les fichiers trouvés.
    La "scrutation" est lancée toutes les 10 secondes par un TTimer, Timer1 dont voici le le gestionnaire d'évènement de son OnTimer :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    procedure TMainAutomel.Timer1Timer(Sender: TObject);
    Var   SousDossier: TSearchRec;
          Rep : Integer ;
          FichierAExpedier : TSearchRec;
          Destinataire : ShortString ;
          T : TextFile ;
          Corps : TStrings ;
          InitMemo : String ;
    begin
      If Compteur=0 then
      Begin
        LabelCompteur.caption := 'Scrutation en cours pour' ;
        LabelCompteur.Update ;
        //Invalidate ;
        //Update ;
        Timer1.Enabled := False ;
        Rep := FindFirst(DossierExpedition.Text+'\*.*', faAnyFile, SousDossier) ;
        While Rep=0 do
        Begin
          If (faDirectory and SousDossier.Attr=faDirectory) then
          If FileExists(DossierExpedition.Text+'\'+SousDossier.Name+'\email.txt') then
          Begin
            // mel du destinataire
            AssignFile(T,DossierExpedition.Text+'\'+SousDossier.Name+'\email.txt') ;
            Reset(T) ;
            Readln(T,Destinataire) ;
            CloseFile(T) ;
     
            // Sauvegarde du mémo sans les pièces jointes
            InitMemo := Memo1.Text ;
     
            // trouver le PREMIER fichier du dossier Expedition\Destinataire
            Rep := FindFirst(DossierExpedition.Text+'\'+SousDossier.Name+'\*.*', faAnyFile, FichierAExpedier) ;
     
            // Boucle FindNEXT, tant que REP=0
            While Rep=0 do
            Begin
              // Si ce n'est pas un sous répértoire
              if (faDirectory and FichierAExpedier.Attr=0) then
              // Si ce n'est pas EMAIL.TXT ...
              If FichierAExpedier.Name<>'email.txt' then
              // ... l'ajouter aux pièces jointes
              Begin
                // Piece ?
                Piece := DossierExpedition.Text+'\'+SousDossier.Name+'\'+FichierAExpedier.Name ;
     
                // Ajout du nom du fichier au corps du message
                If Memo1.Text = '' then Memo1.Text := 'PIECE JOINTE = '+FichierAExpedier.Name
                else Memo1.text  := Memo1.Text+#13+ 'PIECE JOINTE = '+FichierAExpedier.Name ;
     
                // Ajout de la pièce aux pièces jointes
                TIdAttachment.Create(IdMsgSend.MessageParts,Piece);
              End ;
              // FindNEXT
              Rep := FindNext(FichierAExpedier) ;
            End ; // on vient de scruter la totalité du dossier expéditeur
     
            // Corps du message := Memo1
            IdMsgSend.Body.Assign(Memo1.Lines);
     
            // exp du mel
            IdMsgSend.From.Text := UserEmail.Text;
     
            // adresse de réponse
            IdMsgSend.ReplyTo.EMailAddresses := UserEmail.text ;
     
            // destinataire                                                        A dans Outlook
            IdMsgSend.Recipients.EMailAddresses := Destinataire ;
     
            // Objet du mel (sujet)
            IdMsgSend.Subject := 'envoi automatique' ;
     
            // priorité (évident)
            IdMsgSend.Priority := mpNormal ;
     
            // Carbon copy (nul)                                                  CC dans Outlook
            IdMsgSend.CCList.EMailAddresses := '' ;
     
            // Carbon copy invisible                                             CCI dans Outlook
            IdMsgSend.BccList.EMailAddresses := '' ;
     
            // mel du receveur s'il répond
            IdMsgSend.ReceiptRecipient.Text := '';
     
            // Type d'authenfication
            SMTP.AuthenticationType := atNone;
     
            // Hôte SMTP
            SMTP.Host := lesmtp.Text ;
     
            If IdMsgSend.MessageParts.count=0 then
              Continue ; // ---------------------------------------> C O N T I N U E 
     
     
            // Connection
            SMTP.Connect;
     
            try
              // envoi du mel (IdMsgSend)
              SMTP.Send(IdMsgSend);
            finally
              // deconnection
              SMTP.Disconnect;
            end;
            IdMsgSend.Clear ;
            // restauration du meo (corps du message
            Memo1.Text := InitMemo ;
     
            // laisser le processeur traiter les message 'windows) en attente
            Application.ProcessMessages ;
     
            // Libérer de le TSearchRec "FichierAexpedier"
            FindClose(FichierAExpedier) ;
     
            // nouvelle recherche dans le même Dossier
            // ici on va déplacer les fichiers expédiés
            Rep := FindFirst(DossierExpedition.Text+'\'+SousDossier.Name+'\*.*', faAnyFile, FichierAExpedier) ;
     
            // Boucle FindNEXT qui déplace les fichiers
            While Rep=0 do
            Begin
              // Si ce n'est pas un DOSSIER
              if (faDirectory and FichierAExpedier.Attr=0) then
              // Si ce n'est pas le fichier "email.txt"
              If FichierAExpedier.Name<>'email.txt' then
              Begin
                // Création du sous dossier "Expédiés" s'il n'existe pas
                If Not DirectoryExists(DossierExpedition.text+'\'+SousDossier.Name+'\expédiés') then Mkdir(DossierExpedition.text+'\'+SousDossier.Name+'\expédiés');
                // Piece et vers
                Piece := DossierExpedition.Text+'\'+SousDossier.Name+'\'+FichierAExpedier.Name ;
                Vers :=  DossierExpedition.Text+'\'+SousDossier.Name+'\expédiés\'+FichierAExpedier.Name ;
                // Copie du fichier
                If CopieFichier(Piece,Vers) then
                  /// si copié, l'effacer
                  DeleteFile(Piece)
                else
                  // SINON, en informer l'utilisateur
                  BM('','Le Fichier '+Piece+' n''a pas pu être déplacé vers le dossier "expédies"') ;
              End ;
              Rep := FindNext(FichierAExpedier) ;
            End ;
            Memo1.Lines[0] := '' ;
            Application.ProcessMessages ;
            FindClose(FichierAExpedier) ;
          End ;
          Rep := FindNext(SousDossier) ;
        End ;
        FindClose(SousDossier);
        Timer1.Enabled := True ;
        ResetCompteur ;
      end else
      Begin
        Dec(Compteur) ;
        If Compteur<2 then LabelCompteur.caption := '1 seconde avant'
        else LabelCompteur.caption := IntToStr(compteur) + ' secondes avant' ;
      End ;
    end;

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [OL-2007] Limiter la taille des pièces jointes envoyés sur Outlook 2k7
    Par the-morpher dans le forum Outlook
    Réponses: 3
    Dernier message: 28/09/2018, 14h33
  2. Réponses: 9
    Dernier message: 19/10/2012, 09h02
  3. Envoyer formulaire avec pièce jointe
    Par Samanta dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 11/03/2006, 21h35
  4. Réponses: 1
    Dernier message: 03/12/2005, 12h24
  5. TIdMessage et pièce jointe
    Par oli82 dans le forum Web & réseau
    Réponses: 1
    Dernier message: 15/08/2005, 17h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo