如何在Android Studio中获取电子邮件数据?

时间:2017-06-07 12:36:04

标签: java android

为什么每当我尝试运行它时,这会让我的Android应用程序崩溃/什么是将电子邮件数据导入Android应用程序的最佳方法?

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

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());

        }
    }
}

Here is the logcat

1 个答案:

答案 0 :(得分:1)

您的Log.i个调用之一为空消息。 查看您的代码,这可能是catch块中的一个。

尝试记录这样的错误:

Log.i("MainActivity1", "Error: " + e.getMessage());