Envoyer un mail avec Java en utilisant Gmail
Dans ce tutoriel nous allons découvrir comment envoyer un mail avec Java en utilisant le serveur SMTP de Gmail.
L’envoi d’e-mails en java à l’aide de Gmail SMTP et de l’API JavaMail est facile. Le principe d’envoi des e-mails est effectué par l’API JavaMail (en utilisant GMail) et nous avons juste besoin de lui transmettre les paramètres requis. À chaque version, l’API JavaMail devient sophistiquée et l’envoi d’e-mails avec GMail se fait en un clic.
Pour envoyer un mail à l’aide de l’API JavaMail, vous devez charger les deux fichiers jar:
- mailapi.jar
- smtp.jar
Exemple:
import javax.mail.internet.*;
import java.util.Properties;
import javax.mail.*;
class Mail {
public static void send(String from,String pwd,String to,String sub,String msg){
//Propriétés
Properties p = new Properties();
p.put("mail.smtp.host", "smtp.gmail.com");
p.put("mail.smtp.socketFactory.port", "465");
p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
p.put("mail.smtp.auth", "true");
p.put("mail.smtp.port", "465");
//Session
Session s = Session.getDefaultInstance(p,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pwd);
}
});
//composer le message
try {
MimeMessage m = new MimeMessage(s);
m.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
m.setSubject(sub);
m.setText(msg);
//envoyer le message
Transport.send(m);
System.out.println("Message envoyé avec succès");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
//from, password, to, subject, message
Mail.send(
"[email protected]",
"password",
"[email protected]",
"Bienvenu sur WayToLearnX",
"mail de test!"
);
}
}
Sortie:
Message envoyé avec succès
N’oublier pas de changer l’e-mail et le mot de passe. Regardons maintenant comment exécuter le programme :
1- Chargez les fichiers jar:
c:\> set classpath=mailapi.jar;smtp.jar;.;
2- Compiler le fichier java:
c:\> javac Main.java
3- Exécuter le code:
c:\> java Main

Si vous rencontrez ce genre d’erreur:

Vous devez configurer les paramètres de sécurité de GMAIL, en visitant le lien suivant https://myaccount.google.com/security
Défiler la page vers le bas jusqu’à ce que vous trouver Accès moins sécurisé des applications, puis cliquez sur Activer l’accès comme montrer dans l’image ci-dessous:

Cliquez pour activer l’option.

Maintenant vérifier, si tout se passe bien, vous devriez voir le message de réussite « Message envoyé avec succès » dans votre ligne de commande.


/!\ Depuis le 30/05/2022, Google ne permet plus l’accès moins sécurisé
Google a élevé sa sécurité, et je ne trouve plus « accès moins sécurisé des applications »
que faire
bonjour je essaie d’envoyer le message Gmail en java ca me renvoie le message d’erreur:
xception in thread « main » java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
com.sun.mail.util.SocketConnectException
at message_gmail.mails.envoyer(mails.java:47)
at message_gmail.mails.main(mails.java:52)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
com.sun.mail.util.SocketConnectException
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:367)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at message_gmail.mails.envoyer(mails.java:44)
… 1 more
Caused by: com.sun.mail.util.SocketConnectException
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:299)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
… 8 more
Caused by: java.net.UnknownHostException: smtp.gmail.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297)
… 10 more
C:\Users\VANZOYA\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 4 seconds)
xception in thread « main » java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
com.sun.mail.util.SocketConnectException
at message_gmail.mails.envoyer(mails.java:47)
at message_gmail.mails.main(mails.java:52)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
com.sun.mail.util.SocketConnectException
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:367)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at message_gmail.mails.envoyer(mails.java:44)
… 1 more
Caused by: com.sun.mail.util.SocketConnectException
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:299)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
… 8 more
Caused by: java.net.UnknownHostException: smtp.gmail.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297)
… 10 more
C:\Users\VANZOYA\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 4 seconds)
je essaie d’envoyer le message Gmail en java et ca me renvoie le message d’erreur:
run:
Exception in thread « main » java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. For more information, go to
535 5.7.8 https://support.google.com/mail/?p=BadCredentials z10-20020a05600c0a0a00b0040e486bc0dfsm26979590wmp.27 – gsmtp
at message_gmail.mails.envoyer(mails.java:47)
at message_gmail.mails.main(mails.java:52)
Caused by: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. For more information, go to
535 5.7.8 https://support.google.com/mail/?p=BadCredentials z10-20020a05600c0a0a00b0040e486bc0dfsm26979590wmp.27 – gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
at javax.mail.Service.connect(Service.java:367)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at message_gmail.mails.envoyer(mails.java:44)
… 1 more
C:\Users\VANZOYA\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 11 seconds)