-1

I’m using java mail to send automatic mail when a button is pressed. I work on android studio, and i get the error javax.mail.AuthenticationFailedException and I can't find why.

  • I tried changing port to 25 - 587 and 465
  • Id are correct
  • tried on emulator and on real device
  • my gmail account is open to low secure application

here is the code :

Properties props = new Properties();
        props.put("mail.smtp.host" , "smtp.gmail.com");
        props.put("mail.stmp.user" , "horiond@gmail.com");

        //TLS
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.password", "xxxxx");

        //SSL
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session  = Session.getDefaultInstance( props , null);
        String to = "horiond@gmail.com";
        String from = "horiond@gmail.com";
        String subject = "Testing...";
        Message msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(from));
            msg.setRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
            msg.setSubject(subject);
            msg.setText("Working fine..!");
            Transport transport = session.getTransport("smtp");
            transport.connect("smtp.gmail.com" , 465  , "horiov@gmail.com", "xxxx");
            transport.send(msg);
            System.out.println("fine!!");
        }
        catch(Exception exc) {
            System.out.println(exc);
        }
Loic Bch
  • 13
  • 7
  • 1
    Fix these [common JavaMail mistakes](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes), update your post with the new code, and if it still doesn't work, update your post with the [JavaMail debug output](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug). – Bill Shannon Feb 07 '17 at 21:33

1 Answers1

0

You can look at this link in another question. This should be able to help you.

Sending Email in Android using JavaMail API without using the default/built-in app

Community
  • 1
  • 1
Adam Gardner
  • 1,216
  • 1
  • 9
  • 21