0

Here is my code for button click

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String tos=to.getText();
    String froms=from.getText();
    String host="localhost";

    Properties props=System.getProperties();

    props.setProperty("mail.smtp.user", froms);
    props.setProperty("mail.smtp.password", "my password");

    final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    props.setProperty("mail.smtp.host", "smtp.gmail.com");
    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.setProperty("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.port", "465");
    props.setProperty("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.store.protocol", "pop3");
    props.put("mail.transport.protocol", "smtp");
    Session session= Session.getDefaultInstance(props,new Authenticator(){
                         @Override
                         protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(froms, "my password");
                         }});

    try
    { 
        MimeMessage message=new MimeMessage(session);
        message.setFrom(new InternetAddress(froms));
        InternetAddress ip=new InternetAddress(tos);
        message.addRecipient(Message.RecipientType.TO,ip);
        message.setSentDate(new Date());
        message.setSubject(subject.getText());
        message.setText(messageContent.getText());

        Transport.send(message);

        JOptionPane.showMessageDialog(this, "Message sent successfully");
    }
    catch(HeadlessException | MessagingException e)
    {
        System.err.println(e);
        JOptionPane.showMessageDialog(this, e.getMessage());
    }

}

What i'm doing wrong over here? I,m getting following exception:

javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and 534-5.7.14 e8YTsS3Lo3OMrzelDWpE3wPwGVAmVWNUmJ6cR8pp_PJemAB4AD5dQj_bONLKOyNcYUa4Yz 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 s12sm16623355pfd.165 - gsmtp 534-5.7.14 yfLWbfQo_o4JLhaVezEiFbeuFT_Z9hgAu88JaGLSumvl2zpKRaWGgxbp5lhq1JT5yaNGjP 534-5.7.14 kj9zdQPuu8LqdwuDp7esd15LEKE9Wy8348UDQD3ROy4_ViRsDHAoLeLQMSgRrjgT61hO7o 534-5.7.14 RgEb-xZUMd3rCZ_KRXA_xwUj_kjmY> 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 s12sm16623355pfd.165 - gsmtp

pritam parab
  • 85
  • 2
  • 10
  • https://stackoverflow.com/questions/46663/how-can-i-send-an-email-by-java-application-using-gmail-yahoo-or-hotmail This is working for me – Vlad Botolan Feb 11 '18 at 16:19
  • When you followed the instructions in the error message, what happened? Also, note that you should fix all these [common JavaMail mistakes](https://javaee.github.io/javamail/FAQ#commonmistakes) in your code. You also might run into [this problem](https://javaee.github.io/javamail/FAQ#gmailauth). – Bill Shannon Feb 11 '18 at 21:03
  • Thank you so much for your response. The problem was not in the code but google tries prevent third party applications from accessing gmail services. I just allowed third party application access for my email id. – pritam parab Feb 25 '18 at 17:22

0 Answers0