I'm facing a problem with my android project, basically I'm trying to send an email and got the error:
public class ConnectEmail {
private Session session;
private Properties properties;
private final String password;
private final String username;
public ConnectEmail(String username, String password) {
this.username = username;
this.password = password;
}
public boolean getSession() {
boolean result = false;
properties = getProperties();
try {
session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,
password);
}
});
Log.i("Check", "Got session");
result = true;
} catch (Exception e) {
System.out.println(e.toString() + "Session: Unknown Exception");
}
return result;
}
public Properties getProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Log.i("Check", "Got properties");
return props;
}
public void sendEmail(String recipients, String subject, String content) {
String finalString = "";
boolean result = getSession();
if (result) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setDataHandler(handler);
Log.i("Check", "creating message");
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipients));
message.setSubject(subject);
message.setText(content);
Log.i("check", "transport");
Transport transport = session.getTransport("smtp");
Log.i("check", "connecting");
transport.connect("smtp.gmail.com",username , password);
Log.i("check", "wana send");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
Log.i("check", "sent");
} catch (MessagingException e) {
throw new RuntimeException(e);
}} else
System.out.println("Invalid session...");
}
}
And my LogCat says:
11-16 15:33:38.000: E/AndroidRuntime(19426): FATAL EXCEPTION: main
11-16 15:33:38.000: E/AndroidRuntime(19426): java.lang.NoClassDefFoundError: javax.activation.DataHandler
11-16 15:33:38.000: E/AndroidRuntime(19426): at javax.mail.internet.MimeMessage.setContent(MimeMessage.java:1454)
11-16 15:33:38.000: E/AndroidRuntime(19426): at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:1075)
11-16 15:33:38.000: E/AndroidRuntime(19426): at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1493)
11-16 15:33:38.000: E/AndroidRuntime(19426): at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1477)
11-16 15:33:38.000: E/AndroidRuntime(19426): at com.example.emailontime.control.ConnectEmail.sendEmail(ConnectEmail.java:83)
11-16 15:33:38.000: E/AndroidRuntime(19426): at com.example.emailontime.activities.LoginActivity$1.onClick(LoginActivity.java:45)
11-16 15:33:38.000: E/AndroidRuntime(19426): at android.view.View.performClick(View.java:4211)
11-16 15:33:38.000: E/AndroidRuntime(19426): at android.view.View$PerformClick.run(View.java:17362)
11-16 15:33:38.000: E/AndroidRuntime(19426): at android.os.Handler.handleCallback(Handler.java:725)
11-16 15:33:38.000: E/AndroidRuntime(19426): at android.os.Handler.dispatchMessage(Handler.java:92)
11-16 15:33:38.000: E/AndroidRuntime(19426): at android.os.Looper.loop(Looper.java:137)
11-16 15:33:38.000: E/AndroidRuntime(19426): at android.app.ActivityThread.main(ActivityThread.java:5227)
11-16 15:33:38.000: E/AndroidRuntime(19426): at java.lang.reflect.Method.invokeNative(Native Method)
11-16 15:33:38.000: E/AndroidRuntime(19426): at java.lang.reflect.Method.invoke(Method.java:511)
11-16 15:33:38.000: E/AndroidRuntime(19426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-16 15:33:38.000: E/AndroidRuntime(19426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-16 15:33:38.000: E/AndroidRuntime(19426): at dalvik.system.NativeStart.main(Native Method)
I saw some questions and people usually says that the problem is in the build path, I tried to reinstall the mail.jar, clean the project but nothing. Thanks.
Android isn't Java. Please complain to Google. Tell them that Android should be Java Compatible.
In the mean time, if you've added additionnal.jar and activation.jar to your application (as described here) and it's still not working, you're doing something wrong.