java.lang.NoClassDefFoundError:javax.activation.DataHandler

时间:2013-11-16 15:37:46

标签: android javamail noclassdeffounderror datahandler

我的Android项目遇到问题,基本上我正在尝试发送电子邮件并收到错误:

public class ConnectEmail {
    private Session session;
    private Properties properties;
    private final String password;
    private final String username;

    public ConnectEmail(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public boolean getSession() {
        boolean result = false;
        properties = getProperties();
        try {
            session = Session.getDefaultInstance(properties,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username,
                                    password);
                        }
                    });
            Log.i("Check", "Got session");
            result = true;
        } catch (Exception e) {
            System.out.println(e.toString() + "Session: Unknown Exception");
        }

        return result;
    }

    public Properties getProperties() {

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        Log.i("Check", "Got properties");
        return props;
    }

    public void sendEmail(String recipients, String subject, String content) {
        String finalString = "";

        boolean result = getSession();

        if (result) {
            try {
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(username));
                message.setDataHandler(handler);
                Log.i("Check", "creating message");

                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(recipients));
                message.setSubject(subject);
                message.setText(content);

                 Log.i("check", "transport");
                    Transport transport = session.getTransport("smtp");
                    Log.i("check", "connecting");
                    transport.connect("smtp.gmail.com",username , password);
                    Log.i("check", "wana send");
                    transport.sendMessage(message, message.getAllRecipients());
                    transport.close();

                    Log.i("check", "sent");



            } catch (MessagingException e) {
                throw new RuntimeException(e);

        }} else
            System.out.println("Invalid session...");

    }
}

我的LogCat说:

11-16 15:33:38.000: E/AndroidRuntime(19426): FATAL EXCEPTION: main
11-16 15:33:38.000: E/AndroidRuntime(19426): java.lang.NoClassDefFoundError: javax.activation.DataHandler
11-16 15:33:38.000: E/AndroidRuntime(19426):    at javax.mail.internet.MimeMessage.setContent(MimeMessage.java:1454)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:1075)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1493)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1477)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at com.example.emailontime.control.ConnectEmail.sendEmail(ConnectEmail.java:83)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at com.example.emailontime.activities.LoginActivity$1.onClick(LoginActivity.java:45)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at android.view.View.performClick(View.java:4211)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at android.view.View$PerformClick.run(View.java:17362)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at android.os.Handler.handleCallback(Handler.java:725)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at android.os.Looper.loop(Looper.java:137)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at android.app.ActivityThread.main(ActivityThread.java:5227)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at java.lang.reflect.Method.invokeNative(Native Method)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at java.lang.reflect.Method.invoke(Method.java:511)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-16 15:33:38.000: E/AndroidRuntime(19426):    at dalvik.system.NativeStart.main(Native Method)

我看到了一些问题,人们通常会说问题出在构建路径上,我试图重新安装mail.jar,清理项目但没有。 感谢。

2 个答案:

答案 0 :(得分:3)

Android不是Java。请向Google投诉。告诉他们Android应该兼容Java。

与此同时,如果您已将additionnal.jaractivation.jar添加到您的应用程序中(如here所述)并且它仍然无效,那么您做错了。< / p>

答案 1 :(得分:1)

更新:

官方JavaMail for Android https://java.net/projects/javamail/pages/Android

旧的非官方版:Javamail for android https://code.google.com/p/javamail-android/

感谢Bill Shannon