user2626734
user2626734

Reputation: 26

Android studio Javamail API error

Well to make a long story short, I have a dialog fragment where users sign up for the app on. They enter their email and password and its supposed to send them email telling welcome to the app. For some reason I keep getting this error

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.sr116.art_buzz, PID: 5284
                  java.lang.NoClassDefFoundError: javax.activation.DataHandler
                      at javax.mail.internet.MimeMessage.setContent(MimeMessage.java:1516)
                      at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:1183)
                      at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1555)
                      at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1539)
                      at com.sr116.art_buzz.SignUpDialog$2.onClick(SignUpDialog.java:68)
                      at android.view.View.performClick(View.java:4633)
                      at android.view.View$PerformClick.run(View.java:19330)
                      at android.os.Handler.handleCallback(Handler.java:733)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:157)
                      at android.app.ActivityThread.main(ActivityThread.java:5356)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)

                  at dalvik.system.NativeStart.main(Native Method)

Here is my code:

public class SignUpDialog extends DialogFragment
{
    private static final String TAG = "com.sr116.art_buzz";
    //from and to
    final String userName = "[email protected]";
    final String password = "password";




    //Recipients email
    private EditText signUpEmail;
    //Users Password
    private EditText signUpPassword;

    //signUpButton
    Button signUpButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        //In fragments must use view to access ids
        View view = inflater.inflate(R.layout.sign_up_dialog, null);
        signUpEmail = (EditText) view.findViewById(R.id.signUpEmail);
        signUpPassword =(EditText) view.findViewById(R.id.signUpPassword);
        signUpButton = (Button) view.findViewById(R.id.signUpButton);

        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.auth","true");
        props.put("mail.smtp.host","smtp.gmail.com");
        props.put("mail.smtp.port","587");

        final Session session= Session.getInstance(props,
                new javax.mail.Authenticator(){
                   protected PasswordAuthentication getPasswordAuthentication(){
                       return new PasswordAuthentication(userName,password);
                   }
                });

        signUpButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                try{
                    Message message = new MimeMessage(session);
                    message.setFrom(new InternetAddress("[email protected]"));
                    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(signUpEmail.getText().toString()));
                    message.setSubject("Testing");
                    message.setText("Still testing!!!");
                    Transport.send(message);
                }catch (Exception e)
                {
                    throw new RuntimeException(e);
                }
            }
        });
        return view;
    }


}

Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.sr116.art_buzz"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
android {
    packagingOptions {
        pickFirst 'META-INF/LICENSE.txt' // picks the JavaMail license file
    }
}

repositories {
    jcenter()
    maven {
        url "https://maven.java.net/content/groups/public/"
    }
}

dependencies {
    compile 'javax.mail:mail:1.5.0-b01'
    compile 'javax.activation:activation:1.1.1'
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    testCompile 'junit:junit:4.12'
    compile files('libs/javax.mail.jar')
    compile files('libs/activation-1.1.1.jar')
}

Upvotes: 0

Views: 1304

Answers (2)

Bill Shannon
Bill Shannon

Reputation: 29961

No, ignore the other answer. Use the official JavaMail for Android.

Upvotes: 1

peeyush
peeyush

Reputation: 11

There are three libraries that you need to include in your app: mail.jar, activation.jar, and additionnal.jar . It looks like you are missing something that the activation library depends on, and this could be because you are not using the Android port of this library.

Upvotes: 1

Related Questions