我正在尝试构建一个使用 javamail 和 gmail 的 smtp 服务发送电子邮件的应用程序,但是当我运行它时,当我调用 Session.getInstance 时它崩溃了。调试后,它似乎是一个关于 com.sun.mail.util.MailLogger 的 NoClassDefFoundError。我在别处读到我必须添加一个较旧的邮件包才能获得它,但我仍然遇到错误。
这是我在 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 …Run Code Online (Sandbox Code Playgroud)