java – Android Studio:NoClassDefFoundError
I'm trying to build an application that uses JavaMail and Gmail's SMTP service to send e-mail, but when I run it, it will crash when calling session.getinstance. After debugging, it looks like it's NoClassDefFoundError about com.sun.mail.util.maillogger. I read elsewhere. I have to add an older mail package to get it, But I still get errors
This is my content in Android Studio:
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.transport.protocol","smtp");
properties.put("mail.smtp.port", "587");
authenticator = getAuthenticator();
/* What getAuthenticator looks like:
return new javax.mail.Authenticator() {
protected PasswordAuthentication getpasswordAuthentication() {
return new PasswordAuthentication("myemail@gmail.com ", "password");
}
};
*/
// Get the default Session object, with authentication
try {
session = Session.getInstance(properties, authenticator);
} catch (Error e) {
// Unnecessary code for debugging
int hold = 0;
}
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
try {
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(content);
// Send message
Transport.send(message);
} catch (AddressException e) {
throw new Error("bad address");
} catch (MessagingException e){
throw new Error("bad message data");
}
My build looks like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.bsitpa.myfirstapp"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'Meta-INF/LICENSE.txt'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'javax.mail:javax.mail-api:1.5.3'
//compile 'javax.mail:mail:1.5.0-b01'
compile files('mail-1.5.0-b01.jar')
}
Debugging error: "java.lang.noclassdeffounderror: parsing of LCOM / sun / mail / util / maillogger failed"
And you know, I'm completely unfamiliar with Android studio and gradle
Thank you very much, Ryan
resolvent:
I'm not sure if you've solved this problem, but I find you need to use the Android specific java mail library
compile 'com.sun.mail:android-mail:1.5.5'
compile 'com.sun.mail:android-activation:1.5.5'
I encountered your errors and other similar errors using the latest java mail library, but switching to these fixes all of them
https://java.net/projects/javamail/pages/Android