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

API standards et tierces Java Discussion :

[JavaMail] NoClassDefFound en Java EE 5


Sujet :

API standards et tierces Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Juin 2007
    Messages
    35
    Détails du profil
    Informations forums :
    Inscription : Juin 2007
    Messages : 35
    Par défaut [JavaMail] NoClassDefFound en Java EE 5
    Bonjour,
    j'utilise ce bean pour l'envoi de mail
    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
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
     
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Properties;
     
    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Store;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
     
    public class EmailBean {
     
      //defaults
      private final static String DEFAULT_CONTENT = "Unknown content";
     
      private final static String DEFAULT_SUBJECT = "Unknown subject";
     
      private static String DEFAULT_SERVER = null;
     
      private static String DEFAULT_TO = null;
     
      private static String DEFAULT_FROM = null;
      static {
        java.util.ResourceBundle bundle = java.util.ResourceBundle
            .getBundle("com.java2s.mailDefaults");
     
        DEFAULT_SERVER = bundle.getString("DEFAULT_SERVER");
        DEFAULT_TO = bundle.getString("DEFAULT_TO");
        DEFAULT_FROM = bundle.getString("DEFAULT_FROM");
     
        System.out.println("DEFAULT_SERVER: " + DEFAULT_SERVER);
      }
     
      //JavaBean properties
      private String smtpHost;
     
      private String to;
     
      private String from;
     
      private String content;
     
      private String subject;
     
      public void sendMessage() throws Exception {
     
        Properties properties = System.getProperties();
     
        //populate the 'Properties' object with the mail
        //server address, so that the default 'Session'
        //instance can use it.
        properties.put("mail.smtp.host", smtpHost);
     
        Session session = Session.getDefaultInstance(properties);
     
        Message mailMsg = new MimeMessage(session);//a new email message
     
        InternetAddress[] addresses = null;
     
        try {
     
          if (to != null) {
     
            //throws 'AddressException' if the 'to' email address
            //violates RFC822 syntax
            addresses = InternetAddress.parse(to, false);
     
            mailMsg.setRecipients(Message.RecipientType.TO, addresses);
     
          } else {
     
            throw new MessagingException(
                "The mail message requires a 'To' address.");
     
          }
     
          if (from != null) {
     
            mailMsg.setFrom(new InternetAddress(from));
     
          } else {
     
            throw new MessagingException(
                "The mail message requires a valid 'From' address.");
     
          }
     
          if (subject != null)
            mailMsg.setSubject(subject);
     
          if (content != null)
            mailMsg.setText(content);
     
          //Finally, send the mail message; throws a 'SendFailedException'
          //if any of the message's recipients have an invalid address
          Transport.send(mailMsg);
     
        } catch (Exception exc) {
     
          throw exc;
     
        }
     
      }//sendMessage
     
      private void handleMessages(HttpServletRequest request, PrintWriter out)
          throws IOException, ServletException {
     
        HttpSession httpSession = request.getSession();
        String user = (String) httpSession.getAttribute("user");
        String password = (String) httpSession.getAttribute("pass");
        String popAddr = (String) httpSession.getAttribute("pop");
     
        Store popStore = null;
        Folder folder = null;
     
        if (!check(popAddr))
          popAddr = EmailBean.DEFAULT_SERVER;
     
        try {
     
          if ((!check(user)) || (!check(password)))
            throw new ServletException(
                "A valid username and password is required to check email.");
     
          Properties properties = System.getProperties();
     
          Session session = Session.getDefaultInstance(properties);
     
          popStore = session.getStore("pop3");
     
          popStore.connect(popAddr, user, password);
     
          folder = popStore.getFolder("INBOX");
     
          if (!folder.exists())
            throw new ServletException(
                "An 'INBOX' folder does not exist for the user.");
     
          folder.open(Folder.READ_ONLY);
     
          Message[] messages = folder.getMessages();
          int msgLen = messages.length;
     
          if (msgLen == 0)
            out
                .println("<h2>The INBOX folder does not yet contain any email messages.</h2>");
     
          for (int i = 0; i < msgLen; i++) {
            displayMessage(messages[i], out);
            out.println("<br /><br />");
          }
     
        } catch (Exception exc) {
     
          out
              .println("<h2>Sorry, an error occurred while accessing the email messages.</h2>");
          out.println(exc.toString());
     
        } finally {
          try {
            if (folder != null)
              folder.close(false);
     
            if (popStore != null)
              popStore.close();
          } catch (Exception e) {
          }
        }
      }//handleMessages
     
      private void displayMessage(Message msg, PrintWriter out)
          throws MessagingException, IOException {
     
        if (msg != null && msg.getContent() instanceof String) {
     
          if (msg.getFrom()[0] instanceof InternetAddress) {
            out.println("Message received from: "
                + ((InternetAddress) msg.getFrom()[0]).getAddress()
                + "<br />");
          }
          out.println("Message received on: " + msg.getReceivedDate()
              + "<br />");
          out.println("Message content type: " + msg.getContentType()
              + "<br />");
          out.println("Message content type: " + (String) msg.getContent());
        } else {
     
          out
              .println("<h2>The received email message was not of a text content type.</h2>");
     
        }
     
      }//displayMessage
     
      public void setSmtpHost(String host) {
        if (check(host)) {
          this.smtpHost = host;
        } else {
          this.smtpHost = EmailBean.DEFAULT_SERVER;
        }
      }//setTo
     
      public void setTo(String to) {
        if (check(to)) {
          this.to = to;
        } else {
          this.to = EmailBean.DEFAULT_TO;
        }
      }//setTo
     
      public void setFrom(String from) {
        if (check(from)) {
          this.from = from;
        } else {
          this.from = EmailBean.DEFAULT_FROM;
        }
      }//setFrom
     
      public void setContent(String content) {
        if (check(content)) {
          this.content = content;
        } else {
          this.content = EmailBean.DEFAULT_CONTENT;
        }
      }//setContent
     
      public void setSubject(String subject) {
        if (check(subject)) {
          this.subject = subject;
        } else {
          this.subject = EmailBean.DEFAULT_SUBJECT;
        }
      }//setSubject
     
      private boolean check(String value) {
     
        if (value == null || value.equals(""))
          return false;
     
        return true;
      }
    }
    cela marche bien avec j2ee 1.4 mais pas avec J5EE et il affiche cette exception
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/activation/registries/LogSupport
    	at javax.activation.MailcapCommandMap.<init>(MailcapCommandMap.java:140)
    	at javax.activation.CommandMap.getDefaultCommandMap(CommandMap.java:61)
    	at javax.activation.DataHandler.getCommandMap(DataHandler.java:153)
    	at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:611)
    	at javax.activation.DataHandler.writeTo(DataHandler.java:315)
    	at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:248)
    	at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1268)
    	at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2012)
    	at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:1980)
    	at javax.mail.Transport.send(Transport.java:97)
    ...
    est ce que quelqu'un a une idée??
    merci par avance

  2. #2
    Membre éprouvé
    Avatar de _skip
    Homme Profil pro
    Développeur d'applications
    Inscrit en
    Novembre 2005
    Messages
    2 898
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur d'applications
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Novembre 2005
    Messages : 2 898
    Par défaut
    Est-ce que tu as bien le activation.jar dans ton classpath?

  3. #3
    Membre averti
    Inscrit en
    Juin 2007
    Messages
    35
    Détails du profil
    Informations forums :
    Inscription : Juin 2007
    Messages : 35
    Par défaut
    j'ai remarqué que javaee.jar contient tous les package javax.activation.*
    et javax.mail.* mais pas les package qui commence par com.*
    j'ai essayé d'ajouter activation.jar et mail.jar mais cela n'a pas résolu le probleme

Discussions similaires

  1. Réponses: 2
    Dernier message: 23/12/2009, 10h03
  2. Réponses: 3
    Dernier message: 04/12/2007, 22h32
  3. [JavaMail] envoyer mail en java
    Par salim81 dans le forum API standards et tierces
    Réponses: 1
    Dernier message: 12/04/2007, 17h10
  4. [JavaMail] envoi de mails en java
    Par franfr57 dans le forum API standards et tierces
    Réponses: 4
    Dernier message: 11/01/2007, 19h33
  5. JSP javamail java.lang.ClassCastException
    Par itr dans le forum Servlets/JSP
    Réponses: 6
    Dernier message: 14/06/2006, 17h01

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