5

我的主要活动中有一个线程,它将创建一个类的对象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 = "Hello@gmail.com";
    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

我读过这个帖子:NoClassDefFoundError - Eclipse and Android.jar files javamail.jarandjavax.activation.jar位于我的libs文件夹下,但即使我清理项目也会引发异常。

有任何想法吗?

这些是抛出的异常:

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)
4

4 回答 4

5

java为android引入javaMail的新方法:

您只需在 gradle 中添加 thi 行:

 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 邮件...祝您好运。

于 2016-05-15T08:18:14.203 回答
2

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

图片

重要的是,只要您Java.lang.NoClassDefFoundError有一个以上的例外,就是处理该问题的解决方案。

于 2012-08-07T07:16:47.810 回答
0

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

于 2012-08-07T07:16:54.877 回答
0

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

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

于 2014-12-04T08:31:55.057 回答