Android プロジェクトで問題に直面しています。基本的に、メールを送信しようとしてエラーが発生しました。
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...");
}
}
そして、私の LogCat は次のように述べています。
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)
私はいくつかの質問を見て、問題はビルド パスにあると言う人が多く、mail.jar を再インストールしてプロジェクトをクリーンアップしようとしましたが、何もしませんでした。ありがとう。