javax.mail.AuthenticationFailedException Java Mail
J'ai fait un petit programme que d'envoyer un message à mon compte e-mail. La plupart de mon code semble fonctionner correctement. Cependant, j'ai rencontre le AuthenticationFailedException quand je suis mon code frapper transport.send
dans mon programme. Je ne suis pas sûr pourquoi, parce que je pense que j'ai tout mis en place correctement. Voici mon code.
JavaEmail.java
public static void main (String[] args) throws Exception{
//intialize logger
protected static Logger logger = LogManager.getLogger(JavaEmail.class.getName());
//smtp related parameters
private static String smtpUseremail;
private static String smtpReceiverEmailAddress;
private static String smtpUserpassword;
private static String smtpPortnumber;
private static String smtpHost;
private static String emailSubject;
//stack error message variable
private static String message;
try {
logger.info("-----------------------------");
logger.info("---------Starting up---------");
logger.info("-----------------------------");
initialize();
String body = "This is a test";
logger.info("Start to connect to email server");
sendFromGmail(smtpUseremail, smtpUserpassword, smtpReceiverEmailAddress, emailSubject, body);
//System.out.println("error message: " + message);
logger.info("Email Sent");
}
catch (Exception e){
logger.error(e);
//message = e.toString();
throw e;
}
}
//Setting up Email Server
private static void sendFromGmail(String from, String pass, String to, String subject, String body) {
Properties props = PropertiesUtil.loadProperties("main.properties");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPortnumber);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUseremail, smtpUserpassword);
}
});
MimeMessage message = new MimeMessage(session);
try{
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
logger.info("Message has been delievered, check your mail");
}
catch (MessagingException e) {
logger.error("Messaging Exception");
throw new RuntimeException(e);
}
}
//Initializing Properties
private static void initialize() throws Exception {
Properties props = PropertiesUtil.loadProperties("main.properties");
//smtp set up
smtpPortnumber = props.getProperty("portNumber");
smtpReceiverEmailAddress = props.getProperty("recipientEmailAddress");
smtpUseremail = props.getProperty("userEmailAddress");
smtpUserpassword = props.getProperty("userPassword");
smtpHost = props.getProperty("emailHost");
emailSubject = props.getProperty("");
}
}
principal.propriétés
portNumber = 465
recipientEmailAddress = my@gmail.com
userPassword = secert
userEmailAddress = my@gmail.com
emailHost = smtp.gmail.com
erreur
Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvWE
534-5.7.14 HIW4hBxefM-JI-wxqEK6ru2lJstC10IedXvhU_Tzbft1IqPFsot0wfjAWkrX2-gLIEG0NZ
534-5.7.14 V45KWaeagzWTb_v1nAe-BOZSR6YzPHQ7-cYVGYplvcaexmFziL3IMRgkCalmn-5FjreRkm
534-5.7.14 PjtRPd9nmjcosX0ce4DXSGn_y-qGOsW-ehOp2U3eQkSReObXi-a80ywNWiy3QuiV56f61h
534-5.7.14 yz8bmLpugqIYhh7DiNd6HpwvNHAo> 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 ob15sm12759947pdb.52 - gsmtp
at emailNotification.JavaEmail.sendFromGmail(JavaEmail.java:140)
at emailNotification.JavaEmail.main(JavaEmail.java:63)
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvWE
534-5.7.14 HIW4hBxefM-JI-wxqEK6ru2lJstC10IedXvhU_Tzbft1IqPFsot0wfjAWkrX2-gLIEG0NZ
534-5.7.14 V45KWaeagzWTb_v1nAe-BOZSR6YzPHQ7-cYVGYplvcaexmFziL3IMRgkCalmn-5FjreRkm
534-5.7.14 PjtRPd9nmjcosX0ce4DXSGn_y-qGOsW-ehOp2U3eQkSReObXi-a80ywNWiy3QuiV56f61h
534-5.7.14 yz8bmLpugqIYhh7DiNd6HpwvNHAo> 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 ob15sm12759947pdb.52 - 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: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 emailNotification.JavaEmail.sendFromGmail(JavaEmail.java:130)
... 1 more
Veuillez vérifier gmail id/mot de passe est bon ou pas.
Bonjour, je vous remercie pour votre répondre. C'est correct, c'est plus de mon identifiant gmail ac et pw
Veuillez cocher la réponse.
Bonjour, je vous remercie pour votre répondre. C'est correct, c'est plus de mon identifiant gmail ac et pw
Veuillez cocher la réponse.
OriginalL'auteur RedRocket | 2015-08-31
Vous devez vous connecter pour publier un commentaire.
Aller au navigateur puis connectez-vous à votre compte gmail qui essayez-vous dans votre code, puis aller à ce lien https://www.google.com/settings/security/lesssecureapps
Vous verrez
Vous devez cliquer sur
TURN ON
.vous êtes les bienvenus
Je l'ai activée, mais je suis quand même à faire des erreurs pendant des semaines.
J'ai activé cette option, toujours pas de travail, ne sais pas quoi de mal.. peut vous aider. Sa fonctionne parfaitement sur localhost
pas de son pas wrking encore. Son travail sur localhost, mais pas sur la production
OriginalL'auteur Subodh Joshi