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

Java Discussion :

Java email et pièce jointe


Sujet :

Java

  1. #1
    Nouveau Candidat au Club
    Inscrit en
    Février 2011
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Février 2011
    Messages : 2
    Points : 1
    Points
    1
    Par défaut Java email et pièce jointe
    Bonjour,

    Je suis actuellement en train de développer un composant pour envoyer des fichiers par email avec l'interface javax.mail. Je rencontre le problème suivant:

    J'ai écris une classe qui réussie à envoyer un mail mais sans la pièce jointe. Pourtant dans le code j'effectue, il me semble, l'ensemble des opérations pour attacher une pièce jointe au mail.

    Voici un extrait du code:
    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
     
    public boolean sendMail(Mail mail, String subject, String recipientAddress, String recipientCCAddress, String recipientBCCAddress, String senderAddress, Iterator attachments)
        {
            Session session;
            boolean result = false;
            if(!getMailProperties())
                return false;
            session = Session.getInstance(mailProperties, null);
            MimeMessage message;
            Address recipientsTO[];
            message = new MimeMessage(session);
            recipientsTO = getAddressArrayFromAddressString(recipientAddress);
            if(recipientsTO == null)
                return true;
            try
            {
                message.setRecipients(javax.mail.Message.RecipientType.TO, recipientsTO);
                Address recipientsCC[] = getAddressArrayFromAddressString(recipientCCAddress);
                if(recipientsCC != null)
                    message.setRecipients(javax.mail.Message.RecipientType.CC, recipientsCC);
                Address recipientsBCC[] = getAddressArrayFromAddressString(recipientBCCAddress);
                if(recipientsBCC != null)
                    message.setRecipients(javax.mail.Message.RecipientType.BCC, recipientsBCC);
                message.setFrom(new InternetAddress(senderAddress));
                message.setSubject(subject, mail.getEncoding());
                message.setSentDate(new Date());
                String key;
                for(Iterator iterator = mail.getHeaders(); iterator.hasNext(); message.addHeader(key, (String)mail.getHeaderValue(key)))
                    key = (String)iterator.next();
     
                MimeMultipart multipart = new MimeMultipart();
     
                DataHandler mailDataHandler = new DataHandler(mail.getContent());
                BodyPart mailMessageBodyPart = new MimeBodyPart();
                mailMessageBodyPart.setDataHandler(mailDataHandler);
                multipart.addBodyPart(mailMessageBodyPart);
     
                do
                {
                    if(null == attachments || !attachments.hasNext())
                        break;
                    String attachementLocation = (String)attachments.next();
                    File file = new File(attachementLocation);
                    if(file.length() > 0L)
                    {
                        javax.activation.DataSource fileDataSource = new FileDataSource(file);
                        DataHandler attachementDataHandler = new DataHandler(fileDataSource);
                        BodyPart attachementMessageBodyPart = new MimeBodyPart();
                        attachementMessageBodyPart.setDataHandler(attachementDataHandler);
                        attachementMessageBodyPart.setFileName(file.getName());
                        multipart.addBodyPart(attachementMessageBodyPart);
                    }
                } while(true);
     
                message.setContent(multipart);
                Transport.send(message);
                result = true;
            }
            catch(Exception e)
            {
                Logger.log(LogScope.ERROR, "core", this, "core.Exception", e.toString());
                result = false;
            }
            return result;
        }
    Je reçoit bien un email dans outlook mais sans pièce jointe dont voici le contenu:

    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
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    Received: from 95P1X4J ([127.0.0.1]) by 95P1X4J ; Tue, 1 Feb 2011 17:19:20 +0100
    Message-ID: <10094016.1296577160356.JavaMail.isas1@95P1X4J>
    Date: Tue, 1 Feb 2011 17:19:20 +0100 (CET)
    From: support.webstore@test.com
    To: test@localhost.com
    Subject: TEST
    Mime-Version: 1.0
    Content-Type: multipart/mixed;  boundary="----=_Part_1_19222476.1296577160356"
    X-hMailServer-LoopCount: 1
     
    ------=_Part_1_19222476.1296577160356
    Content-Type: text/html;charset=utf-8
    Content-Transfer-Encoding: 7bit
     
    <!--  Template Name: $Workfile:   order_mail.isml  $  -->
     
     
    <!-- TemplateComment :  EMail alert, send to the buyer -->
     
     
    <html>
    <head>
    <meta name="INTERSHOP" content="Powered by INTERSHOP Enfinity"/>
     
    </head>
    <body>
     
    <p><font face="verdana, tahoma, sans-serif" size="2">Dear&nbsp;&nbsp;&nbsp;</font></p>
     
    <p><font face="verdana, tahoma, sans-serif" size="2">Many thanks for placing your order with us.</font></p>
    <p><font face="verdana, tahoma, sans-serif" size="2">is delighted to count you among its customers.</font></p>
    <p><font face="verdana, tahoma, sans-serif" size="2">Here below is a summary of your order. Please check that all the details are correct, print it out and keep a copy as proof of your purchase.</font></p>
    <p><font face="verdana, tahoma, sans-serif" size="2">Your credit card has now been charged with the following amount:</font></p>
    <table border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td><font face="verdana, tahoma, sans-serif" size="2">Order Reference Number :&nbsp;</font></td>
    <td><font face="verdana, tahoma, sans-serif" size="2"><b></b></font></td>
    </tr>
    <tr>
    <td><font face="verdana, tahoma, sans-serif" size="2">Order Status :&nbsp;</font></td>
    <td><font face="verdana, tahoma, sans-serif" size="2"><b>
    APPROVED&nbsp;&nbsp;
    </b></font></td>
    </tr>
    <tr>
    <td><font face="verdana, tahoma, sans-serif" size="2">Order Total:&nbsp;</font></td>
    <td><font face="verdana, tahoma, sans-serif" size="2"><b>
    <!-- -->
    </b></font>
    </td>
    </tr>
    <tr>
    <td><font face="verdana, tahoma, sans-serif" size="2">Order Date :&nbsp;</font></td>
    <td><font face="verdana, tahoma, sans-serif" size="2"><b></b></font></td>
    </tr>
     
     
    </table>
    <BR>
    <table border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td><font face="verdana, tahoma, sans-serif" size="2"><b>Shipping address : </b></font></td>
    <td width="120"></td>
    <td><font face="verdana, tahoma, sans-serif" size="2"><b>Billing address : </b></font></td>
    </tr>
    <tr><td colspan="3"><hr size="1" width="100%"></td></tr>
    <tr>
    <td valign="top">
    <table border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td valign="top" colspan="3">
    <font face="verdana, tahoma, sans-serif" size="2">
    &nbsp;<BR>
     
    <BR>
     
    &nbsp;<BR>
     
    <br>
     
    </font>
    </td>
    </tr>
    </table>
    </td>
    <td></td>
    <td valign="top">
    <table border="0" cellpadding="0" cellspacing="0" style="margin-top:0px;">
    <tr>
    <td colspan="3" valign="top">
    <font face="verdana, tahoma, sans-serif" size="2">
    &nbsp;<BR>
     
    <BR>
     
    &nbsp;<BR>
     
     
    </font>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    <BR>
    <table cellpadding="1" cellspacing="1" border="0" width="737px">
    <tr>
    <td width=10% align=center>
    <font face="verdana, tahoma, sans-serif" size="2"><b>Quantity</b></font>
    </td>
    <td width=40% align=left>
    <font face="verdana, tahoma, sans-serif" size="2"><b>Product Name</b></font>
    </td>
    <td width=20% align=left>
    <font face="verdana, tahoma, sans-serif" size="2"><b>Product Code</b></font>
    </td>
    <td width=15% align=right>
    <font face="verdana, tahoma, sans-serif" size="2"><b>Item Price</b></font>
    </td>
    <td width=15% align=right>
    <font face="verdana, tahoma, sans-serif" size="2"><b>Total Price</b></font>
    </td>
    </tr>
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    <tr><td colspan="5"><hr size="1" width="100%"></td></tr>
    <tr>
    <td colspan=4 valign=middle align=right>
    <font face="verdana, tahoma, sans-serif" size="2">
     Shipping:</font>
    </td>
    <td valign=middle align=right>
    <font face="verdana, tahoma, sans-serif" size="2">
     
    &nbsp;&nbsp;
     
    </font>
    </td>
    </tr>
     
    <tr>
    <td colspan = 4 valign=middle align=right>
    <font face="verdana, tahoma, sans-serif" size="2">
    TOTAL:</font>
    </td>
    <td valign=middle align=right>
    <font face="verdana, tahoma, sans-serif" size="2">
    &nbsp;&nbsp;
    </font>
    </td>
    </tr>
     
     
    </table>
    <p>&nbsp;</p>
    <p><font face="verdana, tahoma, sans-serif" size="2">Your order will be dispatched as quickly as possible.</font></p>
    <p><font face="verdana, tahoma, sans-serif" size="2">To check the status of your order, please go to the Order History in Your Account. For any other assistance, please click <a href="http://95P1X4J.com/is-bin/INTERSHOP.enfinity/WFS/SMC/en_US/-/USD/ViewApplication-DisplayWelcomePage">here</a> and go to Contact Us.</font></p>
    <p><font face="verdana, tahoma, sans-serif" size="2">To remind yourself of our Condition of Sales and in particular our Cancellation and Returns Policy, please click
     
    <a href="http://95P1X4J.com/is-bin/INTERSHOP.enfinity/WFS/SMC/en_US/-/USD/ViewApplication-ShowTemplate?WorkingTemplate=">here</a>
     
    .</font></p>
    <br>
    <p><font face="verdana, tahoma, sans-serif" size="2">Thanks again for shopping with us and we hope you enjoy using webstore.</font></p>
    <p><font face="verdana, tahoma, sans-serif" size="2">Webstore Customer Service</font></p>
    </body>
    </html>
    ------=_Part_1_19222476.1296577160356
    Content-Type: application/octet-stream; name=OLD_WS_DWH_01022011_111645.csv
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename=OLD_WS_DWH_01022011_111645.csv
     
    Company Name : zrgzrg
    Country : zrg
    Email : zrgzrg
    SA server licence : New SA Server licence
    Licence delivery to : Requestor
     
    ------=_Part_1_19222476.1296577160356--
    J'ai essayé de changer les type mime des pièce jointe mais rien n'y fait.

    D'autre part les pièces jointes envoyées sont des fichier .csv que je manipule en passant leur path absolu au niveau de FileDataSource.

    J'espère pouvoir trouver de l'aide ici.

    Cdt,
    Laurent.

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    heuuu
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ------=_Part_1_19222476.1296577160356
    Content-Type: application/octet-stream; name=OLD_WS_DWH_01022011_111645.csv
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename=OLD_WS_DWH_01022011_111645.csv
     
    Company Name : zrgzrg
    Country : zrg
    Email : zrgzrg
    SA server licence : New SA Server licence
    Licence delivery to : Requestor
     
    ------=_Part_1_19222476.1296577160356--
    moi je vois bien une pièce jointe pourtant là

  3. #3
    Nouveau Candidat au Club
    Inscrit en
    Février 2011
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Février 2011
    Messages : 2
    Points : 1
    Points
    1
    Par défaut
    Citation Envoyé par tchize_ Voir le message
    heuuu
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ------=_Part_1_19222476.1296577160356
    Content-Type: application/octet-stream; name=OLD_WS_DWH_01022011_111645.csv
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename=OLD_WS_DWH_01022011_111645.csv
     
    Company Name : zrgzrg
    Country : zrg
    Email : zrgzrg
    SA server licence : New SA Server licence
    Licence delivery to : Requestor
     
    ------=_Part_1_19222476.1296577160356--
    moi je vois bien une pièce jointe pourtant là
    Et bien oui dans le code elle est présente mais lorsque je reçois le mail sous outlook ou même sous gmail j'ai le mail mais sans pièce jointe

Discussions similaires

  1. Envoyer un email avec pièce jointe
    Par LaSirenne dans le forum Développement Web en Java
    Réponses: 7
    Dernier message: 01/11/2010, 20h58
  2. [D6-Outlook] Envoi d'email avec pièces jointes
    Par pepito62 dans le forum Web & réseau
    Réponses: 2
    Dernier message: 23/06/2010, 18h09
  3. [WD-2007] Emailing avec pièce jointe personnalisée
    Par misitbon dans le forum Word
    Réponses: 1
    Dernier message: 09/06/2009, 19h11
  4. [WD12] Envoi Email avec pièces jointes
    Par machou43 dans le forum WinDev
    Réponses: 2
    Dernier message: 07/05/2009, 14h10
  5. Envoi email avec pièce jointe
    Par ouinih dans le forum Modules
    Réponses: 2
    Dernier message: 29/01/2009, 22h17

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