本文介绍了Java.lang.NoClassDefFoundError-JavaMail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我的主要活动中有一个线程,该线程将创建一个 SendMail

I have a thread inside my main activity, which will create an object of the class 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());
    }
}

}

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

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

我已阅读此线程: NoClassDefFoundError-Eclipse和Android .jar文件 javamail.jar javax.activation.jar 位于我的 libs 文件夹下,但这甚至会引发异常如果我清理项目.

I've read this thread: NoClassDefFoundError - Eclipse and Android and the .jar files javamail.jar and javax.activation.jar is located under my libs folder, but this throws an exception even if I clean the project.

有什么想法吗?

这些是抛出的异常:

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)

推荐答案

java引入了适用于android的javaMail的新方法:

java introduce new way for javaMail for android :

您只需要在gradle中添加以下行:

you need just add thi lines in 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'
 }

然后像您一样处理smtp邮件...祝你好运.

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

这篇关于Java.lang.NoClassDefFoundError-JavaMail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 21:49