Java.lang.NoClassDefFoundError - JavaMail

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11840975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:35:53  来源:igfitidea点击:

Java.lang.NoClassDefFoundError - JavaMail

javaandroid

提问by Tobias Moe Thorstensen

I have a thread inside my main activity, which will create an object of the class SendMail

我的主要活动中有一个线程,它将创建一个类的对象 SendMail

package Logic;

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import android.util.Log;

public class SendMail {

String from;
String to;
String subject;
String bodyText;
String fileName;

public SendMail(String to, String fileName, String PCN) {

    this.to = to;
    this.fileName = fileName;
    this.from = "[email protected]";
    this.bodyText = "FILE";
    this.subject = PCN;
}

public void sendMailWithAttatchment() {

    Properties properties = new Properties();
    properties.put("mail.smtp.host", "IP_ADDRESS");
    properties.put("mail.smtp.port", "25");
    Session session = Session.getDefaultInstance(properties, null);

    MimeMessage message = new MimeMessage(session);

    try {

        message.setFrom(new InternetAddress(from));

        message.setRecipient(Message.RecipientType.TO, new InternetAddress(
                to));
        message.setSubject(subject);
        message.setSentDate(new Date());

        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText(bodyText);

        MimeBodyPart attachmentPart = new MimeBodyPart();

        FileDataSource fileDataSource = new FileDataSource(fileName) {
            @Override
            public String getContentType() {
                return "application/octet-stream";

            }
        };

        attachmentPart.setDataHandler(new DataHandler(fileDataSource));
        attachmentPart.setFileName(fileDataSource.getName());

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messagePart);
        multipart.addBodyPart(attachmentPart);
        message.setContent(multipart);
        Transport.send(message);
    } catch (AddressException e) {
        Log.e("ADDRESS_EXCEPTION: ", e.getMessage());
    } catch (MessagingException e) {
        Log.e("MESSAGING_EXCEPTION: ", e.getMessage());
    }
}

}

}

But the compiler throws an Exception saying: Java.lang.NoClassDefFoundError. javax.activation.Datahandler

但是编译器抛出一个异常说: Java.lang.NoClassDefFoundError. javax.activation.Datahandler

I've read this thread: NoClassDefFoundError - Eclipse and Androidand the .jar filesjavamail.jarand javax.activation.jaris located under my libsfolder, but this throws an exception even if I clean the project.

我已经阅读了这个线程:NoClassDefFoundError - Eclipse 和 Android并且.jar filesjavamail.jarjavax.activation.jar位于我的libs文件夹下,但是即使我清理了项目,这也会引发异常。

Any ideas?

有任何想法吗?

These are the exception which is thrown:

这些是抛出的异常:

08-07 10:19:49.870: E/AndroidRuntime(17736): java.lang.NoClassDefFoundError: javax.activation.DataHandler
08-07 10:19:49.870: E/AndroidRuntime(17736):    at javax.mail.internet.MimeBodyPart.setContent(MimeBodyPart.java:647)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:892)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:680)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:668)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at sendMailWithAttatchment(SendMail.java:56)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at sendMailWithAttatchment(SendMail.java:56)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at CreateNistFile(MyActivity.java:530)

回答by Faez Mehrabani

java introduce new way for javaMail for android :

java 为 android 的 javaMail 引入新方法:

you need just add thi lines in gradle:

你只需要在gradle中添加这几行:

 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 'com.sun.mail:android-mail:1.5.5'
     compile 'com.sun.mail:android-activation:1.5.5'
 }

and do the smtp mail like you did... good luck.

并像您一样发送 smtp 邮件...祝您好运。

回答by Praveenkumar

Yes, this because of your .jar file didn't import properly. Just follow my existing answerIt will helps you surely. And, below snapshot is important (It notifies the additional jar files should looks like this image) -

是的,这是因为您的 .jar 文件没有正确导入。只需按照我现有的答案它肯定会帮助你。并且,下面的快照很重要(它通知额外的 jar 文件应该看起来像这个图像)-

image

图片

Important thing is, whenever you'd Java.lang.NoClassDefFoundErrorexception above one is the solution to handle that.

重要的是,每当您遇到Java.lang.NoClassDefFoundError异常时,就是处理该异常的解决方案。

回答by Kamal

If you are using eclipse, right click on project > choose properties> Java build Path> libraries> add Jars and add the jars. Then clean the project

如果您使用的是 Eclipse,请右键单击项目 > 选择属性 > Java 构建路径 > 库 > 添加 jar 并添加 jar。然后清理项目

回答by marmor

Android isn't fully Java-compliant, there's the javamail-android project that adds support for Javamail to Android apps.

Android 并不完全兼容 Java,有 javamail-android 项目为 Android 应用程序添加了对 Javamail 的支持。

make sure you download and add to build path all 3 jarsfrom here: http://code.google.com/p/javamail-android/downloads/list

确保从这里下载并添加到构建路径中的所有 3 个 jarhttp: //code.google.com/p/javamail-android/downloads/list