Bonjour j'utilise l'API javaMail mais quand j’exécute mon code voila le console
mais je ne sais pas comment corriger ce problème.Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException
at testJavaMail.EnvoyerEmail.envoyer(EnvoyerEmail.java:44)
at testJavaMail.EnvoyerEmail.main(EnvoyerEmail.java:49)
Caused by: javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at testJavaMail.EnvoyerEmail.envoyer(EnvoyerEmail.java:41)
... 1 more
voila ma classe
Quelqu'un saurait-il m'indiquer d'où peut venir le problème ?
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 package testJavaMail; import javax.mail.Authenticator; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class EnvoyerEmail { private String username = "ragnar.lodbrok@gmail.com"; private String password = "123456"; public void envoyer() { // Etape 1 : Création de la session Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable","true"); props.put("mail.smtp.host","smtp.gmail.com"); props.put("mail.smtp.port","587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(username, password); } }); try { // Etape 2 : Création de l'objet Message Message message = new MimeMessage(session); message.setFrom(new InternetAddress("ragnar.lodbrok@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("lagertha.wife@gmail.com")); message.setSubject("Test email"); message.setText("Bonjour, ce message est un test ..."); // Etape 3 : Envoyer le message Transport.send(message); System.out.println("Message_envoye"); } catch (MessagingException e) { throw new RuntimeException(e); } } //Etape 4 : Tester la méthode public static void main(String[] args) { EnvoyerEmail test = new EnvoyerEmail(); test.envoyer(); } }
Merci d'avance pour votre aide.
Partager