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

Langage Java Discussion :

Problème Java mail


Sujet :

Langage Java

  1. #1
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Juin 2012
    Messages
    93
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juin 2012
    Messages : 93
    Par défaut Problème Java mail
    salut

    j'ai un application java qui envoyer un messsage a un client

    voila le code

    SendMailUsingAuthentication.java
    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
     
    import javax.mail.*;                                                                                                                
    import javax.mail.internet.*;                                                                                                    
    import java.util.*;                                                                                                                    
     
    public class SendMailUsingAuthentication                                                                               
    {                                                                                                                                            
      private static final String SMTP_HOST_NAME = "smtp.gmail.com";  // for google                
      private static final String SMTP_AUTH_USER = "user mail";             // Username                        
      private static final String SMTP_AUTH_PWD  = "password";          // Password                     
     
     
      // Add List of Email address to who email needs to be sent to                                               
      public void postMail( String recipients[ ], String subject,                                                       
                                String message , String from) throws MessagingException                          
      {                                                                                                                                         
        boolean debug = false;                                                                                                       
         //Set the host smtp address                                                                                               
         Properties props = new Properties();                                                                                
          props.put("mail.smtp.starttls.enable","true");                                                                     
         props.put("mail.smtp.host", SMTP_HOST_NAME);                                                        
         props.put("mail.smtp.auth", "true");                                                                                    
     
        Authenticator auth = new SMTPAuthenticator();                                                                
        Session session = Session.getDefaultInstance(props, auth);                                                 
     
        session.setDebug(debug);                                                                                                  
     
        // create a message                                                                                                            
        Message msg = new MimeMessage(session);                                                                    
     
        // set the from and to address                                                                                            
        InternetAddress addressFrom = new InternetAddress(from);                                             
        msg.setFrom(addressFrom);                                                                                             
     
        InternetAddress[] addressTo = new InternetAddress[recipients.length];                             
        for (int i = 0; i < recipients.length; i++)                                                                               
        {                                                                                                                                      
            addressTo[i] = new InternetAddress(recipients[i]);                                                        
        }                                                                                                                                      
        msg.setRecipients(Message.RecipientType.TO, addressTo);                                             
        // Setting the Subject and Content Type                                                                            
        msg.setSubject(subject);                                                                                                  
        msg.setContent(message, "text/plain");                                                                              
        Transport.send(msg);                                                                                                       
     }                                                                                                                                        
     
     
    /**                                                                                                                                      
    * SimpleAuthenticator is used to do simple authentication                                                     
    * when the SMTP server requires it.                                                                                    
    */                                                                                                                                        
    private class SMTPAuthenticator extends javax.mail.Authenticator                                        
    {                                                                                                                                          
     
            @Override                                                                                                                   
        public PasswordAuthentication getPasswordAuthentication()                                             
        {                                                                                                                                      
            String username = SMTP_AUTH_USER;                                                                    
            String password = SMTP_AUTH_PWD;                                                                     
            return new PasswordAuthentication(username, password);                                             
        }                                                                                                                                       
    } 
    }
    MainForm.java
    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
    251
    252
    253
    254
    255
     
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;
     
     
    /**
     *
     * @author  KAI
     */
    public class MainForm extends javax.swing.JFrame {
      private static  String emailMsgTxt      = "";
      private static  String emailSubjectTxt  = "";
      private static  String emailFromAddress = "";
     
      // Add List of Email address to who email needs to be sent to
      private static  String[] emailList = new String[10];
     
        /** Creates new form MainForm */
        public MainForm() {
            initComponents();
        }
    public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
    {
        boolean debug = false;
     
         //Set the host smtp address
         Properties props = new Properties();
         props.put("mail.smtp.starttls.enable","true");
         props.put("mail.smtp.host", "smtp.gmail.com");
     
        // create some properties and get the default Session
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);
     
        // create a message
        Message msg = new MimeMessage(session);
     
        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
     
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++)
        {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);
     
     
        // Optional : You can also set your custom headers in the Email if you Want
        msg.addHeader("MyHeaderName", "myHeaderValue");
     
        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            jPanel1 = new javax.swing.JPanel();
            jPanel2 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            txto = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
            txtsubject = new javax.swing.JTextField();
            jLabel3 = new javax.swing.JLabel();
            txtfrom = new javax.swing.JTextField();
            jLabel4 = new javax.swing.JLabel();
            jScrollPane1 = new javax.swing.JScrollPane();
            txtmessage = new javax.swing.JTextArea();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
     
            jButton1.setText("send Mail");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
     
            jLabel1.setText("To");
     
            txto.setText("");
     
            jLabel2.setText("Subject");
     
            txtsubject.setText("");
     
            jLabel3.setText("From:");
     
            txtfrom.setEditable(false);
            txtfrom.setText("kanzari90@gmail.com");
     
            jLabel4.setText("Message:");
     
            txtmessage.setColumns(20);
            txtmessage.setRows(5);
            txtmessage.setText("");
            jScrollPane1.setViewportView(txtmessage);
     
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1)
                                .addComponent(jLabel3)
                                .addComponent(jLabel4))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtfrom, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                                .addComponent(txtsubject, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                                .addComponent(txto, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                                .addComponent(jScrollPane1)))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(158, 158, 158)
                            .addComponent(jButton1)))
                    .addContainerGap(40, Short.MAX_VALUE))
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(23, 23, 23)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(txtsubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(txtfrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel4)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(jButton1)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(39, 39, 39)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(23, Short.MAX_VALUE))
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
     
            pack();
        }// </editor-fold>
     
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
     
          emailMsgTxt      = txtmessage.getText();
         emailSubjectTxt  = txtsubject.getText();
    emailFromAddress = txtfrom.getText();
     
      // Add List of Email address to who email needs to be sent to
    StringBuffer sb=new StringBuffer(txto.getText());
    StringTokenizer st=new StringTokenizer(txto.getText());
    int i=0;
    while(st.hasMoreTokens()){
        emailList[i]=st.nextToken(",");
        System.err.println(emailList[i]);
        i++;
    }
     
      String emailReceipeint[]=new String[i];
      for(int j=0;j<i;j++){
      emailReceipeint[j]=emailList[j];
      System.out.println("Actually emails are "+j);
      }
     
     
     
        SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
            try {
                smtpMailSender.postMail(emailReceipeint, emailSubjectTxt, emailMsgTxt, emailFromAddress);
            } catch (MessagingException ex) {
                Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            }
        System.out.println("Sucessfully Sent mail to All Users");
     
     
    }                                        
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new MainForm().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextField txtfrom;
        private static javax.swing.JTextArea txtmessage;
        private static javax.swing.JTextField txto;
        private static javax.swing.JTextField txtsubject;
        // End of variables declaration
     
    }

    comment faire pour lorsque :
    le client est voir le message , je reçois un message qui dit le client est ouvre le message.
    mais lorsque le client ne voir pas le message l'application envoyer automatique un message dans 4 jours "Nous vous avons envoyé un message au cours des quatre derniers jours" .

    S'il vous plaît aidez-moi j'ai besoin à la projet fin d'etude

  2. #2
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 582
    Par défaut
    Il n'y a pas, en général, de moyen de savoir si quelqu'un a lu un mail ou non.

    Sur GMail, il n'y en a pas, du tout. GMail ne gère pas les accusés de réception, et désactive tous les mouchards.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

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

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

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Tout au mieux tu peux mettre une adresse d'expéditeur correcte dans tes message pour recevoir les message d'erreur (type utilisateur n'existe pas, server non accessible, etc) mais comme dit thelvin, impossible de savoir si ça a été ouvert ou non, impossible de mettre un "timeout" configurable. Un email ce n'est pas un envoi recommandé de laposte

  4. #4
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Juin 2012
    Messages
    93
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juin 2012
    Messages : 93
    Par défaut
    Merci pour l'information

    Pouvez-vous m'aider pour que l'application envoyer un message automatiquement chaque 4 jour a la dernière client qui j'ai envoyé un message !!

    Merci d'avance pour votre aide ^^

Discussions similaires

  1. Problème avec java mail
    Par abdel_aat dans le forum API standards et tierces
    Réponses: 0
    Dernier message: 02/09/2009, 13h18
  2. Problème envoi mail avec java
    Par poupouce5 dans le forum Entrée/Sortie
    Réponses: 2
    Dernier message: 12/06/2008, 23h22
  3. Problème de réception de mail avec java mail
    Par osiris23 dans le forum API standards et tierces
    Réponses: 2
    Dernier message: 26/05/2008, 21h43
  4. Installation Java Mail
    Par Benzz dans le forum API standards et tierces
    Réponses: 1
    Dernier message: 18/01/2006, 09h42
  5. problème java run time environment
    Par abrmed dans le forum Autres Logiciels
    Réponses: 7
    Dernier message: 19/08/2005, 13h27

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