Envoyer un mail via mon java application

Signaler
-
BunoCS
Messages postés
14742
Date d'inscription
lundi 11 juillet 2005
Statut
Modérateur
Dernière intervention
17 février 2020
-
je viens pour envoyer un mail via mon java application , aucune erreur apparait dans ce code mais ne marche pas
voilà le code :
package teamviewr;

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 SendEmail{
 public  static boolean envoie=false;
 static String s ;
public SendEmail(String text) {
s=text;}
public static boolean sendEmail1(String email,String nom,String passwrd){
  final String username = "***@***";
  final String password = "9azerty6";
  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 {

   Message message = new MimeMessage(session);
   message.setFrom(new InternetAddress(username));
   message.setRecipients(Message.RecipientType.TO,
    InternetAddress.parse(email));
   message.setSubject("Login et mot de passe");
   message.setText(" Bonjour , "+nom
    + s+".\n\n\n"+
     "login :  "+email+
    "\n password : "+passwrd);

   Transport.send(message);

   System.out.println("Done");
   
   envoie =true;

  } catch (MessagingException e) {
   throw new RuntimeException(e);
  }
  return envoie;
 }

public static void main(String[] arguments) {
new SendEmail("nawress");
}}



EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ICI

Merci d'y penser dans tes prochains messages.

3 réponses

Messages postés
14742
Date d'inscription
lundi 11 juillet 2005
Statut
Modérateur
Dernière intervention
17 février 2020
91
Hello,

Sauf erreur de ma part, tu n'appelles pas la méthode d'envoi d'email...
this is the main i do ;
public static void main(String[] arguments) {
SendEmail s= new SendEmail("nawress");
if ( s.sendEmail1("*****@**.com","****","*****")==true)
System.out.println("done");
}}

but there error !

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs0
534-5.7.14 ppP5bRIJokROojS_lusVepyiVOTiSRFobmnmdvwlB_2y26irnywzQvJOWyt4sEYNyzuczB
534-5.7.14 xEFyaX7CVxenHsErfrv_8COELgBXYlG-b_7ZRsBYALWGvsS7-LMGnFD_0i128d3tb-iK65
534-5.7.14 T6EFnFEkoUna2kCcw2xrua1HeSj7rxUiaxcDVAIMjlYXbPXkLqbWi85YxBE-D11vletn4G
534-5.7.14 VNg7NVGD0h7ZGieRDmMfGS9gXF0sY> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 by7sm10323419wjc.18 - gsmtp

at teamviewr.SendEmail.sendEmail1(SendEmail.java:51)
at teamviewr.SendEmail.main(SendEmail.java:58)
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs0
534-5.7.14 ppP5bRIJokROojS_lusVepyiVOTiSRFobmnmdvwlB_2y26irnywzQvJOWyt4sEYNyzuczB
534-5.7.14 xEFyaX7CVxenHsErfrv_8COELgBXYlG-b_7ZRsBYALWGvsS7-LMGnFD_0i128d3tb-iK65
534-5.7.14 T6EFnFEkoUna2kCcw2xrua1HeSj7rxUiaxcDVAIMjlYXbPXkLqbWi85YxBE-D11vletn4G
534-5.7.14 VNg7NVGD0h7ZGieRDmMfGS9gXF0sY> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 by7sm10323419wjc.18 - gsmtp

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at teamviewr.SendEmail.sendEmail1(SendEmail.java:44)
... 1 more
Messages postés
14742
Date d'inscription
lundi 11 juillet 2005
Statut
Modérateur
Dernière intervention
17 février 2020
91
Caused by: javax.mail.AuthenticationFailedException
Tu as un problème d'authentification...Fais des recherches à ce niveau