为什么每当我试图运行它时,崩溃我的android应用程序/将电子邮件数据导入android应用程序的最佳方式是什么?如何在Android Studio中获取电子邮件数据?

该应用程序本身只包含一个按钮点击 - 我已经得到了下面的代码在eclipse中工作,我试图运行它在手机上,而不是一个模拟器,所以我不再收到网络错误。

package com.example.marc.emailtest;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import java.util.Properties;

import javax.mail.Address;

import javax.mail.Folder;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.NoSuchProviderException;

import javax.mail.Session;

import javax.mail.Store;

import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

public void buttonOnClick(View v){

String host = "pop.gmail.com";// change accordingly

String StoreType = "pop3";

String user = "******@gmail.com";// I purposefully hid these

String password = "********";// I purposefully hid these

try {

//create properties field

Properties properties = new Properties();

properties.put("mail.pop3.host", host);

properties.put("mail.pop3.port", "995");

properties.put("mail.pop3.starttls.enable", "true");

Session emailSession = Session.getDefaultInstance(properties);

//create the POP3 store object and connect with the pop server

Store store = emailSession.getStore("pop3s");

store.connect(host, user, password);

//create the folder object and open it

Folder emailFolder = store.getFolder("INBOX");

emailFolder.open(Folder.READ_ONLY);

// retrieve the messages from the folder in an array and print it

Message[] messages = emailFolder.getMessages();

System.out.println("messages.length---" + messages.length);

for (int i = 0, n = messages.length; i < n; i++) {

Message message = messages[i];

Log.i("MainActivity1", "---------------------------------");

Log.i("MainActivity1", "Email Number " + (i + 1));

Log.i("MainActivity1", "Subject: " + message.getSubject());

Log.i("MainActivity1", "From: " + message.getFrom()[0]);

Log.i("MainActivity1", "Text: " + message.getContent().toString());

}

//close the store and folder objects

emailFolder.close(false);

store.close();

} catch (NoSuchProviderException e) {

e.printStackTrace();

Log.i("MainActivity1", e.getMessage());

} catch (MessagingException e) {

e.printStackTrace();

Log.i("MainActivity1", e.getMessage());

} catch (Exception e) {

e.printStackTrace();

Log.i("MainActivity1", e.getMessage());

}

}

}

91a9c0878f0e0f51778b5d4fc1b95c91.png

+1

添加logcat的。 –

+0

确实会添加LogCat的输出,这会显示出错。我不知道这是否由javax.mail包处理,但我的第一个猜测是,您尝试在主线程上执行与网络相关的事情,这在Android上是不允许的。 尝试将其包装到一个AsyncTask中,看看是否有效。 –

+0

已添加logcat –

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐