4

I'm using the following code to send emails in my Android application:

    m_properties = new Properties();
    m_properties.put("mail.smtp.host", "smtp.gmail.com");
    m_properties.put("mail.smtp.socketFactory.port", "465");
    m_properties.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory");
    m_properties.put("mail.smtp.auth", "true");
    m_properties.put("mail.smtp.port", "465");

    m_Session = Session.getDefaultInstance(m_properties,
                    new Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(userName,
                                    passWord);
                        }

                    });

    m_simpleMessage = new MimeMessage(m_Session);

    m_fromAddress = new InternetAddress(userName);
    m_toAddress = new InternetAddress(eRecipient);

    m_simpleMessage.setFrom(m_fromAddress);
    m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
    m_simpleMessage.setSubject(eSubject);
    m_simpleMessage.setContent(eBody, "text/plain");

    Transport.send(m_simpleMessage);

It works absolutely fine until I obfuscate my code. Emails then fail to send, throwing the error: javax.mail.nosuchproviderexception smtp

I've tried various set ups in my proguard.cfg such as: -keep class javax.mail.** { *; }

But nothing works. If I turn off the obfuscation, it works fine again.

Any ideas why this would be happening?

Thanks in advance.

EDIT: My Proguard config file

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-verbose


-libraryjars "C:\Program Files\Android\android-sdk\platforms\android-15\android.jar"
-libraryjars "C:\Documents and Settingslibs\commons-io-2.4.jar"
-libraryjars "C:\Documents and Settings\libs\mail.jar"
-libraryjars "C:\Documents and Settingslibs\activation.jar"
-libraryjars "C:\Documents and Settings\libs\additionnal.jar"
-libraryjars "C:\Documents and Settings\libs\dropbox-android-sdk-1.3.1.jar"
-libraryjars "C:\Documents and Settings\libs\microsoft-translator-java-api-0.6.1-jar-with-dependencies.jar"
-libraryjars "C:\Documents and Settings\pingpongboss-StandOut-5c5cbe9\library\bin\standout.jar"
-libraryjars "C:\Documents and Settings\android-uitableview\android-uitableview\bin\br_com_dina_ui.jar"

-dontpreverify


!code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-optimizationpasses 5
-allowaccessmodification

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class com.android.vending.licensing.ILicensingService

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}


-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}


-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep public class com.winterwell.jgeoplanet.** {
  public protected *;
}

-keep public class winterwell.jwitter.** {
  public protected *;
}

-keep public class winterwell.json.** {
  public protected *;
}

-keep class com.winterwell.** { *; }

-keep class winterwell.** { *; }

-keep class com.faceture.google.** { *; }
-keep class com.faceture.google.play.** { *; }
-keep class com.faceture.google.play.domain.** { *; }
-keep class com.faceture.http.** { *; }
-keep class com.faceture.rest.** { *; }
-keep class com.faceture.google.gson.** { *; }
-keep class com.google.gson.** { *; }


-dontwarn android.support.**
-dontwarn org.apache.commons.codec.binary.**
-dontwarn org.apache.harmony.awt.**
-dontwarn javax.activation.**
-dontwarn com.sun.mail.imap.protocol.**

-keepattributes Signature

-keepattributes *Annotation*


-keep class sun.misc.Unsafe { *; }

-keep class com.google.gson.examples.android.model.** { *; }

The warnings are removed above due to these errors:

Warning: javax.activation.CommandInfo: can't find referenced class java.beans.Beans
Warning: com.sun.mail.imap.protocol.IMAPSaslAuthenticator: can't find referenced class javax.security.sasl.Sasl
+ can't find referenced class javax.security.sasl.SaslClient
+ can't find referenced class javax.security.sasl.SaslException
+ can't find referenced class javax.security.auth.callback.NameCallback
+ can't find referenced class javax.security.sasl.RealmCallback
brandall
  • 6,094
  • 4
  • 49
  • 103
  • what are you doing to obfuscate your code ? – deefactorial Oct 27 '12 at 04:00
  • Nothing out of the ordinary... A few additions from the standard Proguard suggested configuration. I'm referencing the Jars in the normal way. Was there something specific you meant? Thanks – brandall Oct 27 '12 at 04:07
  • it sounds to me like your alterations are making a change that causes the error, without knowing what they are I can't do much about them. – deefactorial Oct 27 '12 at 04:12
  • looks like progard is removing some of the classes you are using have you tried to include more classes with the -keep public class like all your import classes – deefactorial Oct 27 '12 at 04:17
  • I've added my config - I need to work out how to format it though! Thanks for your help. I -kept all of the classes that were referencing the above code, but it still failed... – brandall Oct 27 '12 at 04:26

3 Answers3

9

I solved this problem by including the additional Proguard config detailed below. I got the solution from this answer which has further information.

-keep class javamail.** {*;}
-keep class javax.mail.** {*;}
-keep class javax.activation.** {*;}

-keep class com.sun.mail.dsn.** {*;}
-keep class com.sun.mail.handlers.** {*;}
-keep class com.sun.mail.smtp.** {*;}
-keep class com.sun.mail.util.** {*;}
-keep class mailcap.** {*;}
-keep class mimetypes.** {*;}
-keep class myjava.awt.datatransfer.** {*;}
-keep class org.apache.harmony.awt.** {*;}
-keep class org.apache.harmony.misc.** {*;}
Community
  • 1
  • 1
brandall
  • 6,094
  • 4
  • 49
  • 103
1

I don't know what Proguard is, but it sounds like it's pulling the class files out of mail.jar and adding them to your jar. Unfortunately, it's not including the configuration files that are in the mail.jar file. You need the files from META-INF in the mail.jar file. Or better yet, don't repackage mail.jar, just use it as is.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Thanks for your reply. I'm not repackaging the mail.jar, I'm using it as it came from this link https://code.google.com/p/javamail-android/downloads/list - So everything should remain referenced. – brandall Oct 27 '12 at 04:59
0

Usually this error occurs because of incorrect "mail.jar".

Solution:

1] Check if "mail.jar" is in your classpath.

2] If its there in the classpath,Check for the latest version of "mail.jar" and then include it in your classpath.

You can check and download the latest version from,

http://www.oracle.com/technetwork/java/index-138643.html

ashubhargave
  • 230
  • 2
  • 14