Search code examples
androidsmtpjakarta-mail

Java mail fails on Marshmallow and above, but succeeds on Lollipop and below


I build an APP to send a short message through Java mail API with a guide from this . It works on my android 5.0.2 phone. But when I deploy it on android 6 or higher, it always failed on authentication. There is no special change for mail setting compared with the guide mentioned above. I list some of them for information.

    public MailSender(String user, String password, String mailhost) {
    this.user = user;
    this.password = password;
    Log.i("SMTP_mail", mailhost);

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.host", mailhost);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.password", password);
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.quitwait", "false");

    session = Session.getDefaultInstance(props, this);
    session.setDebug(true);
}

Also, I added permissions check at runtime with a new request from API 23.

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.INTERNET},
                4);
    }

And AndroidManifest.xml is as following:

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

With help from the guide for debugging JavaMail, I collect the logs for success and failure.
Success on Android 5:
enter image description here

And failure on Android 6:
enter image description here

The app code is the same for these two logs except the platforms are different.
Also I notice that there are some certificate failure seen on android 6 log, but from stackoverview search, it seems not a big deal. I am not sure whether it impact anything.

E/NativeCrypto: ssl=0x7ba1e98480 cert_verify_callback x509_store_ctx=0x7ba1cfbc98 arg=0x0 ssl=0x7ba1e98480 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
I/System.out: gba_cipher_suite:TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA

Could anyone help with this issue? It bothers me for several days. Your help is very appreciated.


Solution

  • Finally, I get the reason why “535 5.7.1 Authentication failed” was reported. It is so unexpected because of the pre-installed ime of the device Meizu Note3(m3 note) with Flyme 6.3.0.2A. After I change to google ime and type password again, the email could work successfully. It is so sad for this reason which cost a lot of time to troubleshooting and end with a so tricky tiny point.

    Thanks for all guys for comments and suggestions for this ticket.