JavaMail for Android works for the debug version but not the release version

20 Views Asked by At

I am using javax.mail targeting Android:

implementation 'com.sun.mail:android-mail:1.6.7'
implementation 'com.sun.mail:android-activation:1.6.7'

Code:

try {
    // Create a Properties object
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "mail.domainfoo.com");
    props.put("mail.smtp.port", 587);
    props.put("mail.debug", "true");

    // Create a Session object
    Session session = Session.getInstance(props, new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(SENDER_EMAIL, SENDER_PASSWORD);
        }
    });

    // Create a Message object
    Message message = new MimeMessage(session);

    // Set the sender and recipients
    message.setFrom(new InternetAddress(SENDER_EMAIL));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmails));

    message.setText("some text");
    // Send the email
    Transport.send(message);
} catch (Exception ex) {

}

This works flawlessly and reliably for the debug version of the app but Transport.send(message) throws the following exception for the release version:

Exception: smtp
Class: javax.mail.NoSuchProviderException
 Stack trace: javax.mail.NoSuchProviderException: smtp
        at javax.mail.Session.getService(Session.java:856)
        at javax.mail.Session.getTransport(Session.java:772)
        at javax.mail.Session.getTransport(Session.java:713)
        at javax.mail.Session.getTransport(Session.java:693)
        at javax.mail.Session.getTransport(Session.java:750)
        at javax.mail.Transport.send0(Transport.java:225)
        at javax.mail.Transport.send(Transport.java:100)

Could anyone offer a tip on the possible causes of this exception? I wonder if this is related to R8 used to build the release version.

Update 2023-09-27

I found the solution - adding the following to Proguard file:

-keep class javax.mail.** { *; }
-keep class javax.activation.** {*;}
-keep class com.sun.mail.** { *; }
0

There are 0 best solutions below