Mailapi.jar: faut-il utiliesr un maven project ?

Goahould_nt Messages postés 21 Date d'inscription vendredi 21 avril 2023 Statut Membre Dernière intervention 27 janvier 2024 - Modifié le 1 mai 2023 à 18:51
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 - 1 mai 2023 à 07:41

Je crée une application de gestion des stocks qui doit envoyer par email une commande :

j'essaie d'envoyé un mail avec la classe main suivante :

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;

import java.util.Properties;

public class Main {

public static void main(String[] args) throws MessagingException {

// Recipient's email ID needs to be mentioned.

String to = "***@***";//change accordingly

// Sender's email ID needs to be mentioned

String from = "***@***";//change accordingly

final String username = "***@***";//change accordingly

final String password = "soleil626$";//change accordingly

// Assuming you are sending email through relay.jangosmtp.net

String host = "smtp.gmail.com";

Properties props = new Properties();

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.starttls.enable", "true");

props.put("mail.smtp.host", host);

props.put("mail.smtp.port", "587");

// Get the Session object.

Session session = Session.getInstance(props,

new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(username, password);

}

});

try {

// Create a default MimeMessage object.

Message message = new MimeMessage(session);

// Set From: header field of the header.

message.setFrom(new InternetAddress(from));

// Set To: header field of the header.

message.setRecipients(Message.RecipientType.TO,

InternetAddress.parse(to));

// Set Subject: header field

message.setSubject("Testing Subject");

// Now set the actual message

message.setText("Hello, this is sample for to check send "

+ "email using JavaMailAPI ");

// Send message

Transport.send(message);

System.out.println("Sent message successfully....");

} catch (MessagingException e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

}

et j'obtiens l'erreur suivante lorsque je compile dans eclipse :

Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataSource

at Main.main(Main.java:41)

Caused by: java.lang.ClassNotFoundException: javax.activation.DataSource

at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)

at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)

at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)

... 1 more

Faut-il utiliser un projet maven dans eclipse pour utiliser l'api javax.mail et ses packages ? si oui, comment faire ?

4 réponses

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 127
30 avril 2023 à 11:53

Bonjour,

Il te manque une dépendance que tu peux soit configurer avec un projet Maven, soit télécharger le jar et le configurer manuellement.

https://central.sonatype.com/artifact/javax.activation/activation/1.1.1


0
Goahould_nt Messages postés 21 Date d'inscription vendredi 21 avril 2023 Statut Membre Dernière intervention 27 janvier 2024
30 avril 2023 à 16:45

Merci pour le lien. Mais je ne comprends pas ou je dois coller ce snippet :

<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

 Est-ce dans le ficher .classpath ? Parce que le créer un projet maven, c'est un peu compliqué pour moi ! Puis-je utiliser l'api javax.mail sans créer de projet maven ?

0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 127
30 avril 2023 à 18:34

Le bloc de code XML que tu montres permet de rajouter une dépendance dans un projet Maven (dans le fichier pom.xml)

Mais dans le lien que je te donnais tu peux aussi télécharger directement le jar

(ils ont changé l'interface graphique récemment et je trouve le lien plus difficile à trouver qu'avant, mais il est bien là)

https://repo1.maven.org/maven2/javax/activation/activation/1.1.1/

0
Goahould_nt Messages postés 21 Date d'inscription vendredi 21 avril 2023 Statut Membre Dernière intervention 27 janvier 2024
30 avril 2023 à 23:34

Merci beaucoup, je n'avais pas vu le lien. Maintenant, j'ai encore des exceptions, mais je devrais y arriver !

0
Goahould_nt Messages postés 21 Date d'inscription vendredi 21 avril 2023 Statut Membre Dernière intervention 27 janvier 2024
1 mai 2023 à 00:16

En fait j'ai toujours une exception :

javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted.

avec le code suivants : 

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;

import java.util.Properties;

public class SMTPC {

public static void main(String[] args) throws MessagingException {

// Recipient's email ID needs to be mentioned.

String to = "***@***";//change accordingly

// Sender's email ID needs to be mentioned

String from = "***@***";//change accordingly

final String username = "Jo Monnat";//change accordingly

final String password = "xxxxxxx";//change accordingly

// Assuming you are sending email through relay.jangosmtp.net

String host = "smtp.gmail.com";

Properties props = new Properties();

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.starttls.enable", "true");

props.put("mail.smtp.host", host);

props.put("mail.smtp.port", "587");

props.put("mail.transport.protocol", "smtp");

props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

props.put("mail.smtp.debug", "true");

props.put("mail.smtp.ssl.protocols", "TLSv1.2");

// Get the Session object.

Session session = Session.getInstance(props,

new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(username, password);

}

});

try {

// Create a default MimeMessage object.

Message message = new MimeMessage(session);

// Set From: header field of the header.

message.setFrom(new InternetAddress(from));

// Set To: header field of the header.

message.setRecipients(Message.RecipientType.TO,

InternetAddress.parse(to));

// Set Subject: header field

message.setSubject("Testing Subject");

// Now set the actual message

message.setText("Hello, this is sample for to check send "

+ "email using JavaMailAPI ");

// Send message

Transport.send(message);

System.out.println("Sent message successfully....");

} catch (MessagingException e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

}

J'ai revérifié les paramètres (props et params de connexion username et password), ils sont justes !

Alors quelqu'un sait de quoi pourrais venir cette exception ? 

0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 127
1 mai 2023 à 07:41

Une petite recherche Google sur le message d'erreur te donne plein de résultats puisque c'est GMail qui te renvoie ce message.

https://www.google.com/search?q=535-5.7.8+Username+and+Password+not+accepted

Apparemment il faudrait configurer ton compte gmail pour autoriser l'envois de mail via un programme tiers comme tu essayes de faire, par défaut c'est désactivé.

https://support.google.com/mail/answer/7126229

Attention : fais tes tests sur une adresse gmail jetable, si ton programme fonctionne mal tu pourrais bloquer le compte associé, ce serait dommage de le faire sur une adresse mail qui compte...

0
Rejoignez-nous