1

Sorry for my english! I get a error when I try to send an e-mail from my gmail account, with the JavaMail API in Android. I get this error: AuthenticationFailedException. I tried following: the email address with the password are correct (I tested also with an another mail address). The settings in google are correct. I tested with port 465, then I get another exception. I have searched for 3 days on the internet and have found nothing that has helped.

public class MailSending extends AsyncTask<Void, Void, Void> {
private String mail;
private String pass;
private String an;
private String betreff;
private String nachricht;

public MailSending(String mail, String pass, String an, String betreff, String nachricht) {
    this.mail = mail;
    this.pass = pass;
    this.an = an;
    this.betreff = betreff;
    this.nachricht = nachricht;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
}

@Override
protected Void doInBackground(Void... params) {
    final String host = "smtp.gmail.com";
    Properties props = new Properties();

    props.put("mail.smtp.host", host);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.port", "587");

    Session ses = Session.getInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(mail, pass);
        }
    });

    try {
        MimeMessage message = new MimeMessage(ses);
        InternetAddress addressTo = new InternetAddress(an);
        InternetAddress addressFrom = new InternetAddress(mail);

        message.setFrom(addressFrom);
        message.setRecipient(Message.RecipientType.TO, addressTo);
        message.setSubject(betreff);
        message.setContent(nachricht, "text/plain");

        Transport.send(message);
        Log.d("Nachricht:", "erfolgreich gesendet!");

    } catch (MessagingException e) {
        e.printStackTrace();
    }
    return null;
}

W/System.err: javax.mail.AuthenticationFailedException

W/System.err: at javax.mail.Service.connect(Service.java:319)

W/System.err: at javax.mail.Service.connect(Service.java:169)

W/System.err: at javax.mail.Service.connect(Service.java:118)

W/System.err: at javax.mail.Transport.send0(Transport.java:188)

W/System.err: at javax.mail.Transport.send(Transport.java:118)

W/System.err: at com.example.tuba.login.MailSending.doInBackground(MailSending.java:72)

W/System.err: at com.example.tuba.login.MailSending.doInBackground(MailSending.java:25)

W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)

W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)

W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)

W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

W/System.err: at java.lang.Thread.run(Thread.java:841)

3 Answers 3

1

Did you find the JavaMail FAQ?

Most likely you need to enable less secure apps, but without seeing the details of your exception it's hard to know for sure.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. trying to connect to host "smtp.gmail.com", port 587, isSSL false connected to host "smtp.gmail.com", port: 587. I get this information, after I add session.setDebug(true). What mean this?
1

Maybe this problem get caused by Gmail account protection. Just click the link below and disable some security settings.

--

Vielleicht wird dieses Problem durch den Gmail-Schutz ausgelöst. Überprüfe deine Sicherheitseinstellungen und deaktiviere einige Einstellungen.

Link: https://www.google.com/settings/security/lesssecureapps

2 Comments

I deactivate the security. But this not solved the problem :(
Are you using 2 Factor-Authorization? If yes I think this can also be the problem because you need to send the code first.
0

I find a missing parameter from Properties class. Please add the following code and try again.

props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

According to the documentation:

https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.