我正在为 android OS 构建一个电子邮件发送应用程序。我的 libs 文件夹中有三个 jar:activation.jar、additional.jar 和 mail.jar。我还将这三个 jar 包含在项目的构建路径中,作为“引用库”。当我运行此应用程序并尝试发送邮件时,我在 LogCat 上收到以下错误。
致命异常:java.lang.NoClassDefFoundError:javax.activation.DataHandler
我完全被难住了,因为许多其他人都有同样的 NoClassDefFoundError 错误。由于我的类构建路径中包含所需的 jar 库,因此我认为它可能是运行时错误,如链接中所述:https ://stackoverflow.com/a/34419/3576562 注意:我遵循了这个 android 电子邮件教程:http ://www.chengxuyuans.com/qa/android/85326.html
活动.java:
Button dialogButton1 = (Button) dialog.findViewById(R.id.SendButton);
dialogButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(), "Forgot-Password-Request button", Toast.LENGTH_LONG).show();
final EditText email = (EditText) dialog.findViewById(R.id.EditEmail);
if(email.getText() != null && email.getText().toString().trim().length() > 0){//check for email written.
Runnable run = new Runnable() {
public void run() {
SendPasswordRequestEmail(email.getText().toString());
dialog.dismiss();
}
};
(new Thread(run)).start();
}
}
});
public void SendPasswordRequestEmail(String emailAddress){
try{
GMailSender sender = new GMailSender("nsdonaldwebmail@gmail.com", "email password");
sender.sendMail("This is Subject",
"This is Body",
"nsdonaldwebmail@gmail.com",
"nsdonaldwebmail@gmail.com");
}catch (Exception ex) {
Log.e("MailChimp", "SendEmail Method-Exception forgot password email: " + ex.getMessage());
showResult("SendEmail Method-Exception forgot password email: "+ex.getMessage(), "Error");
ex.printStackTrace();
}
}
GMailSender.java:
package com.example.proactiveregistrationapplication;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new com.example.proactiveregistrationapplication.JSSEProvider());
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
JSSEProvider.java:
package com.example.proactiveregistrationapplication;
import java.security.AccessController;
import java.security.Provider;
public final class JSSEProvider extends Provider {
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
Logcat 错误:
04-17 16:15:04.395: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.395: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.415: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory
04-17 16:15:04.415: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error
04-17 16:15:04.435: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory
04-17 16:15:04.435: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error
04-17 16:15:04.435: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory
04-17 16:15:04.435: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error
04-17 16:15:04.445: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory
04-17 16:15:04.445: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error
04-17 16:15:04.455: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory
04-17 16:15:04.455: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error
04-17 16:15:04.455: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory
04-17 16:15:04.455: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.475: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.475: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.475: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.475: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.475: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.485: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler>
04-17 16:15:04.485: E/AndroidRuntime(26895): FATAL EXCEPTION: Thread-6719
04-17 16:15:04.485: E/AndroidRuntime(26895): Process: com.example.proactiveregistrationapplication, PID: 26895
04-17 16:15:04.485: E/AndroidRuntime(26895): java.lang.NoClassDefFoundError: javax.activation.DataHandler
04-17 16:15:04.485: E/AndroidRuntime(26895): at com.example.proactiveregistrationapplication.GMailSender.sendMail(GMailSender.java:52)
04-17 16:15:04.485: E/AndroidRuntime(26895): at com.example.proactiveregistrationapplication.UnsubscribeActivity.SendPasswordRequestEmail(UnsubscribeActivity.java:216)
04-17 16:15:04.485: E/AndroidRuntime(26895): at com.example.proactiveregistrationapplication.UnsubscribeActivity$3$1$1.run(UnsubscribeActivity.java:83)
04-17 16:15:04.485: E/AndroidRuntime(26895): at java.lang.Thread.run(Thread.java:818)
04-17 16:15:04.635: D/ViewRootImpl(26895): MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 759) or=1
04-17 16:15:04.655: I/Timeline(26895): Timeline: Activity_idle id: android.os.BinderProxy@7b3d479 time:49039028
04-17 16:15:04.945: D/ViewRootImpl(26895): #3 mView = null
04-17 16:15:04.965: E/WindowManager(26895): android.view.WindowLeaked: Activity com.example.proactiveregistrationapplication.UnsubscribeActivity has leaked window com.android.internal.policy.PhoneWindow$DecorView{4f688cf V.E...... R......D 0,0-1002,565} that was originally added here
04-17 16:15:04.965: E/WindowManager(26895): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:565)
04-17 16:15:04.965: E/WindowManager(26895): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:326)
04-17 16:15:04.965: E/WindowManager(26895): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-17 16:15:04.965: E/WindowManager(26895): at android.app.Dialog.show(Dialog.java:350)
04-17 16:15:04.965: E/WindowManager(26895): at com.example.proactiveregistrationapplication.UnsubscribeActivity$3.onClick(UnsubscribeActivity.java:99)
04-17 16:15:04.965: E/WindowManager(26895): at android.view.View.performClick(View.java:5697)
04-17 16:15:04.965: E/WindowManager(26895): at android.widget.TextView.performClick(TextView.java:10826)
04-17 16:15:04.965: E/WindowManager(26895): at android.view.View$PerformClick.run(View.java:22526)
04-17 16:15:04.965: E/WindowManager(26895): at android.os.Handler.handleCallback(Handler.java:739)
04-17 16:15:04.965: E/WindowManager(26895): at android.os.Handler.dispatchMessage(Handler.java:95)
04-17 16:15:04.965: E/WindowManager(26895): at android.os.Looper.loop(Looper.java:158)
04-17 16:15:04.965: E/WindowManager(26895): at android.app.ActivityThread.main(ActivityThread.java:7224)
04-17 16:15:04.965: E/WindowManager(26895): at java.lang.reflect.Method.invoke(Native Method)
04-17 16:15:04.965: E/WindowManager(26895): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-17 16:15:04.965: E/WindowManager(26895): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)