JavaMail: Java.awt.datatransfer.Transferable 不存在
JavaMail: Java.awt.datatransfer.Transferable doesn't exist
我不打算提出问题,但我没有解决这个问题的方法!
我在从 IMAP 服务器接收消息时遇到问题。
错误显示 "Caused by: java.lang.ClassNotFoundException: Didn't find class "java.awt.datatransfer.Transferable" on path..." 这是我的代码:
enter String test(){
String all="";
try{
class Runner extends AsyncTask<Object, String, String> {
@Override
protected String doInBackground(Object... params) {
Looper.prepare();
String all ="";
try{
Message[] msgs = ReceiveMail("imap.gmail.com","993","USER@gmail.com","PASS"); // After passing this line, error logging says error is in this line!
for(Message m: msgs){
all+=m.getSubject()+"\n"+m.getContent().toString()+"\n\n"; // Error shows here, but popups above
}
return all;
}catch (Exception e){
e.printStackTrace();
}
Looper.loop();
return all;
}
}
Runner r = new Runner();
all = r.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this).get();
}catch (Exception e){
e.printStackTrace();
}
return all;
}
private Message[] ReceiveMail(String host,String port,String user,String pass){
try{
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imaps.host", host);
props.setProperty("mail.imaps.port", port);
props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.imaps.socketFactory.fallback", "false");
Session imapSession = Session.getInstance(props);
Store store = imapSession.getStore("imaps");
store.connect(host, user, pass);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
return inbox.getMessages();
}catch (Exception e){
e.printStackTrace();
//log_all("ReceiveMail function: "+e.getMessage());
}
return null;
}
有什么问题?
注意当我不使用AsyncTask时出现错误"Network in main thread"。
解决方案:
下载 these 并将它们添加为库。
从 module settings
.
的依赖项选项卡中删除 javax.*
这样就解决了。
因为它没有找到一些 class 也许您缺少外部 jar?
编辑
您可以使用 JavaMail API 来处理您的电子邮件任务。 JavaMail API 在 JavaEE 包中可用,其 jar 可供下载。遗憾的是,它不能直接在 Android 应用程序中使用,因为它使用与 Android 完全不兼容的 AWT 组件。 (这就是您收到此错误的原因)
您可以在以下位置找到 JavaMail 的 Android 端口:http://code.google.com/p/javamail-android/
将 jar 添加到您的应用程序并使用 SMTP 方法
JavaMail API Reference Implementation version 1.5.5 and later have built in support for Android and include support for OAuth2。
根据文档:
Android does not provide a Java Compatible runtime and so can't run the standard JavaMail distribution. Instead, a special version of JavaMail is available for Android. This special version of JavaMail depends on a special version of the JavaBeans Activation Framework.
You can try out this version by adding the following to your build.gradle file for your Android application:
android {
packagingOptions {
pickFirst 'META-INF/LICENSE.txt' // picks the JavaMail license file
}
}
repositories {
jcenter()
maven {
url "https://maven.java.net/content/groups/public/"
}
}
dependencies {
compile 'com.sun.mail:android-mail:1.5.6'
compile 'com.sun.mail:android-activation:1.5.6'
}
我不打算提出问题,但我没有解决这个问题的方法!
我在从 IMAP 服务器接收消息时遇到问题。
错误显示 "Caused by: java.lang.ClassNotFoundException: Didn't find class "java.awt.datatransfer.Transferable" on path..." 这是我的代码:
enter String test(){
String all="";
try{
class Runner extends AsyncTask<Object, String, String> {
@Override
protected String doInBackground(Object... params) {
Looper.prepare();
String all ="";
try{
Message[] msgs = ReceiveMail("imap.gmail.com","993","USER@gmail.com","PASS"); // After passing this line, error logging says error is in this line!
for(Message m: msgs){
all+=m.getSubject()+"\n"+m.getContent().toString()+"\n\n"; // Error shows here, but popups above
}
return all;
}catch (Exception e){
e.printStackTrace();
}
Looper.loop();
return all;
}
}
Runner r = new Runner();
all = r.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this).get();
}catch (Exception e){
e.printStackTrace();
}
return all;
}
private Message[] ReceiveMail(String host,String port,String user,String pass){
try{
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imaps.host", host);
props.setProperty("mail.imaps.port", port);
props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.imaps.socketFactory.fallback", "false");
Session imapSession = Session.getInstance(props);
Store store = imapSession.getStore("imaps");
store.connect(host, user, pass);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
return inbox.getMessages();
}catch (Exception e){
e.printStackTrace();
//log_all("ReceiveMail function: "+e.getMessage());
}
return null;
}
有什么问题?
注意当我不使用AsyncTask时出现错误"Network in main thread"。
解决方案:
下载 these 并将它们添加为库。
从 module settings
.
的依赖项选项卡中删除 javax.*
这样就解决了。
因为它没有找到一些 class 也许您缺少外部 jar?
编辑
您可以使用 JavaMail API 来处理您的电子邮件任务。 JavaMail API 在 JavaEE 包中可用,其 jar 可供下载。遗憾的是,它不能直接在 Android 应用程序中使用,因为它使用与 Android 完全不兼容的 AWT 组件。 (这就是您收到此错误的原因)
您可以在以下位置找到 JavaMail 的 Android 端口:http://code.google.com/p/javamail-android/
将 jar 添加到您的应用程序并使用 SMTP 方法
JavaMail API Reference Implementation version 1.5.5 and later have built in support for Android and include support for OAuth2。 根据文档:
Android does not provide a Java Compatible runtime and so can't run the standard JavaMail distribution. Instead, a special version of JavaMail is available for Android. This special version of JavaMail depends on a special version of the JavaBeans Activation Framework.
You can try out this version by adding the following to your build.gradle file for your Android application:
android {
packagingOptions {
pickFirst 'META-INF/LICENSE.txt' // picks the JavaMail license file
}
}
repositories {
jcenter()
maven {
url "https://maven.java.net/content/groups/public/"
}
}
dependencies {
compile 'com.sun.mail:android-mail:1.5.6'
compile 'com.sun.mail:android-activation:1.5.6'
}