Android Javamail 工作室 API 错误

Android studio Javamail API 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.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)

这是我的代码:

public class SignUpDialog extends DialogFragment
{
    private static final String TAG = "com.sr116.art_buzz";
    //from and to
    final String userName = "email@gmail.com";
    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@gmail.com"));
                    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')
}

您需要在应用中包含三个库:mail.jar、activation.jar 和 additionnal.jar。看起来您缺少激活库所依赖的某些东西,这可能是因为您没有使用该库的 Android 端口。

否,忽略其他答案。使用官方JavaMail for Android.