我已经阅读了很多关于 android 电子邮件的博客和论坛...我现在的目标是从POP3 邮件服务器下载收到的电子邮件...有大量的信息如何发送电子邮件,但同时几乎没有关于如何以编程方式接收带有附件的电子邮件并将附件保存在SD 卡上的信息...
请,任何帮助表示赞赏!
例如,最接近的含义是... Android:正确下载/保存电子邮件附件
这段代码在java中的PC上工作!!!
package com.tsysv.mail;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import com.sun.mail.pop3.POP3Store;
public class EmailReceiveTest {
public static void main(String[] args) {
String mailPop3Host = "pop3.***.**";
String mailStoreType = "pop3";
String mailUser = "*****";
String mailPassword = "******";
receiveEmail(mailPop3Host, mailStoreType, mailUser, mailPassword);
}
public static void receiveEmail(String pop3Host, String storeType, String user, String password) {
try {
Properties properties = new Properties();
properties.put("mail.pop3.host", pop3Host);
Session emailSession = Session.getDefaultInstance(properties);
POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);
emailStore.connect(user, password);
Folder emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
System.out.println("==============================");
System.out.println("Email #" + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Text: " + message.getContent().toString());
}
emailFolder.close(false);
emailStore.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
但是在 Android 上实现它会在 StackTrace 中出现这样的错误......
02-15 10:21:55.044: W/System.err(2687): javax.mail.MessagingException: Connect failed;
02-15 10:21:55.044: W/System.err(2687): nested exception is:
02-15 10:21:55.044: W/System.err(2687): java.net.SocketException: Permission denied
02-15 10:21:55.044: W/System.err(2687): at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:176)
02-15 10:21:55.054: W/System.err(2687): at javax.mail.Service.connect(Service.java:291)
02-15 10:21:55.054: W/System.err(2687): at javax.mail.Service.connect(Service.java:172)
02-15 10:21:55.054: W/System.err(2687): at javax.mail.Service.connect(Service.java:192)
02-15 10:21:55.054: W/System.err(2687): at m.tsysv.pop3mail.MainActivity.receiveEmail(MainActivity.java:67)
02-15 10:21:55.054: W/System.err(2687): at m.tsysv.pop3mail.MainActivity$1.onClick(MainActivity.java:53)
02-15 10:21:55.054: W/System.err(2687): at android.view.View.performClick(View.java:2408)
02-15 10:21:55.054: W/System.err(2687): at android.view.View$PerformClick.run(View.java:8817)
02-15 10:21:55.054: W/System.err(2687): at android.os.Handler.handleCallback(Handler.java:587)
02-15 10:21:55.054: W/System.err(2687): at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 10:21:55.054: W/System.err(2687): at android.os.Looper.loop(Looper.java:144)
02-15 10:21:55.054: W/System.err(2687): at android.app.ActivityThread.main(ActivityThread.java:4937)
02-15 10:21:55.054: W/System.err(2687): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 10:21:55.054: W/System.err(2687): at java.lang.reflect.Method.invoke(Method.java:521)
02-15 10:21:55.054: W/System.err(2687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-15 10:21:55.054: W/System.err(2687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-15 10:21:55.054: W/System.err(2687): at dalvik.system.NativeStart.main(Native Method)
02-15 10:21:55.054: W/System.err(2687): Caused by: java.net.SocketException: Permission denied
02-15 10:21:55.064: W/System.err(2687): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
02-15 10:21:55.064: W/System.err(2687): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
02-15 10:21:55.064: W/System.err(2687): at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
02-15 10:21:55.064: W/System.err(2687): at java.net.Socket.checkClosedAndCreate(Socket.java:873)
02-15 10:21:55.064: W/System.err(2687): at java.net.Socket.connect(Socket.java:1020)
02-15 10:21:55.064: W/System.err(2687): at java.net.Socket.connect(Socket.java:997)
02-15 10:21:55.064: W/System.err(2687): at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
02-15 10:21:55.064: W/System.err(2687): at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
02-15 10:21:55.064: W/System.err(2687): at com.sun.mail.pop3.Protocol.<init>(Protocol.java:98)
02-15 10:21:55.064: W/System.err(2687): at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:227)
02-15 10:21:55.064: W/System.err(2687): at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:172)
02-15 10:21:55.064: W/System.err(2687): ... 16 more