Java 如何在Android上获取电子邮件?
我正在尝试开发一个应用程序,从不是谷歌的POP3服务器获取电子邮件,我面临着许多问题。Java 如何在Android上获取电子邮件?,java,android,email,jakarta-mail,Java,Android,Email,Jakarta Mail,我正在尝试开发一个应用程序,从不是谷歌的POP3服务器获取电子邮件,我面临着许多问题。 我正在使用JavaMail库和下面的教程Point教程。他们的pop3示例在Eclipse/desktop上运行得很好,但当我在Android上移动代码时,它就不起作用了,我很沮丧。 在logcat中,我得到了所有的错误堆,其中首先声明 W/System.err﹕ android.os.NetworkOnMainThreadException,即使我使用的是AsyncTask(可能不正确) 有没有一种方法可以
我正在使用JavaMail库和下面的教程Point教程。他们的pop3示例在Eclipse/desktop上运行得很好,但当我在Android上移动代码时,它就不起作用了,我很沮丧。
在logcat中,我得到了所有的错误堆,其中首先声明 W/System.err﹕ android.os.NetworkOnMainThreadException,即使我使用的是AsyncTask(可能不正确) 有没有一种方法可以使AsyncTask正常工作 还有,有没有一种方法可以让我不用像K-9 Mail这样的专业应用程序来做类似的事情 如果有人感兴趣,请输入代码:
public class FetchPop extends AsyncTask{
public static void fetch(String pop3Host, String storeType, String user,
String password) {
try {
// create properties field
Properties properties = new Properties();
properties.put("mail.store.protocol", "pop3");
properties.put("mail.pop3.host", pop3Host);
properties.put("mail.pop3.port", "995");
properties.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
// emailSession.setDebug(true);
// create the POP3 store object and connect with the pop server
Store store = emailSession.getStore("pop3s");
store.connect(pop3Host, user, password);
// create the folder object and open it
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
// retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
Log.d("No. messages:", messages.length + ""); //just the number at first
/*for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
writePart(message);
String line = reader.readLine();
if ("YES".equals(line)) {
message.writeTo(System.out);
} else if ("QUIT".equals(line)) {
break;
}
}*/
// close the store and folder objects
emailFolder.close(false);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} /*catch (IOException e) {
e.printStackTrace();
}*/ catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected Object doInBackground(Object[] params) {
String host = "pop.gmail.com";// I tried google's pop
String mailStoreType = "pop3";
String username =
"myusername";// change accordingly
String password = "notmyrealpass";// change accordingly
//Call method fetch
fetch(host, mailStoreType, username, password);
Log.d("mytag","done!");
return null;
}
public void GO() {
doInBackground(null);
}
/*
* This method checks for content-type
* based on which, it processes and
* fetches the content of the message
*/
public static void writePart(Part p) throws Exception {
if (p instanceof Message)
//Call methos writeEnvelope
writeEnvelope((Message) p);
/* System.out.println("----------------------------");
System.out.println("CONTENT-TYPE: " + p.getContentType());*/
//check if the content is plain text
if (p.isMimeType("text/plain")) {
System.out.println("This is plain text");
System.out.println("---------------------------");
System.out.println((String) p.getContent());
}
//check if the content has attachment
else if (p.isMimeType("multipart/*")) {
System.out.println("This is a Multipart");
System.out.println("---------------------------");
Multipart mp = (Multipart) p.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++)
writePart(mp.getBodyPart(i));
}
//check if the content is a nested message
else if (p.isMimeType("message/rfc822")) {
System.out.println("This is a Nested Message");
System.out.println("---------------------------");
writePart((Part) p.getContent());
}
//check if the content is an inline image
else if (p.isMimeType("image/jpeg")) {
System.out.println("--------> image/jpeg");
Object o = p.getContent();
InputStream x = (InputStream) o;
// Construct the required byte array
int i;
byte[] bArray = new byte[0];
System.out.println("x.length = " + x.available());
while ((i = (int) ((InputStream) x).available()) > 0) {
int result = (int) (((InputStream) x).read(bArray));
if (result == -1)
i=0;
bArray = new byte[x.available()];
break;
}
FileOutputStream f2 = new FileOutputStream("/tmp/image.jpg");
f2.write(bArray);
}
else if (p.getContentType().contains("image/")) {
System.out.println("content type" + p.getContentType());
File f = new File("image" + new Date().getTime() + ".jpg");
DataOutputStream output = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(f)));
com.sun.mail.util.BASE64DecoderStream test =
(com.sun.mail.util.BASE64DecoderStream) p
.getContent();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = test.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
}
else {
Object o = p.getContent();
if (o instanceof String) {
System.out.println("This is a string");
System.out.println("---------------------------");
System.out.println((String) o);
}
else if (o instanceof InputStream) {
System.out.println("This is just an input stream");
System.out.println("---------------------------");
InputStream is = (InputStream) o;
is = (InputStream) o;
int c;
while ((c = is.read()) != -1)
System.out.write(c);
}
else {
System.out.println("This is an unknown type");
System.out.println("---------------------------");
System.out.println(o.toString());
}
}
}
/*
* This method would print FROM,TO and SUBJECT of the message
*/
public static void writeEnvelope(Message m) throws Exception {
System.out.println("This is the message envelope");
System.out.println("---------------------------");
Address[] a;
// FROM
if ((a = m.getFrom()) != null) {
for (int j = 0; j < a.length; j++)
System.out.println("FROM: " + a[j].toString());
}
// TO
if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++)
System.out.println("TO: " + a[j].toString());
}
// SUBJECT
if (m.getSubject() != null)
System.out.println("SUBJECT: " + m.getSubject());
}
}
公共类FetchPop扩展异步任务{
publicstaticvoidfetch(字符串pop3Host、字符串storeType、字符串user、,
字符串(密码){
试一试{
//创建属性字段
属性=新属性();
properties.put(“mail.store.protocol”、“pop3”);
properties.put(“mail.pop3.host”,pop3Host);
properties.put(“mail.pop3.port”,“995”);
properties.put(“mail.pop3.starttls.enable”、“true”);
会话emailSession=Session.getDefaultInstance(属性);
//emailSession.setDebug(true);
//创建POP3存储对象并连接pop服务器
Store Store=emailSession.getStore(“pop3s”);
store.connect(pop3Host、用户、密码);
//创建文件夹对象并将其打开
文件夹emailFolder=store.getFolder(“收件箱”);
emailFolder.open(文件夹只读);
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
系统(in),;
//从数组中的文件夹中检索邮件并打印
Message[]messages=emailFolder.getMessages();
Log.d(“No.messages:,messages.length+”);//只需输入第一个数字
/*for(int i=0;iimage/jpeg”);
对象o=p.getContent();
输入流x=(输入流)o;
//构造所需的字节数组
int i;
字节[]bArray=新字节[0];
System.out.println(“x.length=“+x.available());
而((i=(int)((InputStream)x).available())>0){
int结果=(int)((InputStream)x.read(bArray));
如果(结果==-1)
i=0;
bArray=新字节[x.available()];
打破
}
FileOutputStream f2=新的FileOutputStream(“/tmp/image.jpg”);
f2.写入(bArray);
}
else if(p.getContentType().contains(“image/”)){
System.out.println(“内容类型”+p.getContentType());
文件f=新文件(“图像”+新日期().getTime()+”.jpg”);
DataOutputStream输出=新的DataOutputStream(
新的BufferedOutputStream(新的FileOutputStream(f));
com.s
public class FetchPop extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
String host = "pop.gmail.com";// I tried google's pop
String mailStoreType = "pop3";
String username = params[0]; //passed in through the execute() method
String password = params[1]; //passed in through the execute() method
//Call method fetch
fetch(host, mailStoreType, username, password);
Log.d("mytag", "done!");
return null;
}
}
//First get the username and password from the user through the UI
String user = "myusername"; // change accordingly
String pass = "notmyrealpass"; // change accordingly
new FetchPop().execute(user, pass);