【问题标题】:Is it possible to use Javamail to receive mails in Android是否可以在 Android 中使用 Javamail 接收邮件
【发布时间】:2017-10-17 17:23:38
【问题描述】:

这几天我在学习 Javamail,关注这个网站: http://www.tutorialspoint.com/javamail_api/

我测试了发送并添加了一些额外的东西,因为它是在 Android 上运行的!

但是当我尝试按照接收电子邮件教程进行操作时,情况已经完全改变了,这让我感到奇怪..

是否可以制作此代码: https://www.tutorialspoint.com/javamail_api/javamail_api_fetching_emails.htm

适用于android但使用XML接口?!

【问题讨论】:

  • 是的,可以在 Android 上使用 JavaMail;确保您遵循说明here。如果还是不行,JavaMail FAQ中有调试提示。您还可以在JavaMail web site 上找到许多示例程序,但您需要针对 Android 进行调整。抱歉,我不知道“使用 XML 接口”在这种情况下是什么意思,因为我不是 Android 专家。

标签: android email gmail jakarta-mail


【解决方案1】:

您可以使用以下代码:

public static void receiveEmail(String pop3Host, String storeType, user, String password) {
        try {
            //1) get the session object  
            Properties properties = new Properties();
            properties.put("mail.pop3.host", pop3Host);
            Session emailSession = Session.getDefaultInstance(properties);

            //2) create the POP3 store object and connect with the pop server  
            POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);
            emailStore.connect(user, password);

            //3) create the folder object and open it  
            Folder emailFolder = emailStore.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);

            //4) retrieve the messages from the folder in an array and print it  
            Message[] messages = emailFolder.getMessages();
            for (int i = 0; i < messages.length; i++) {
                Message message = messages[i];
                System.out.println("---------------------------------");
                System.out.println("Email Number " + (i + 1));
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Text: " + message.getContent().toString());
            }

            //5) close the store and folder objects  
            emailFolder.close(false);
            emailStore.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

调用你的方法传递一些参数:

String host = "pop.gmail.com";//change accordingly  
String mailStoreType = "pop3";  
String username= "example@gmail.com";  
String password= "xxxxx";//change accordingly  

receiveEmail(host, mailStoreType, username, password); 

来源:Steps for receiving the email using JavaMail API

【讨论】:

  • 我在 AsyncTask 扩展类中对其进行了测试,并将一个值传递给 ListView 作为对列表的测试,但我收到的是 null
  • @Sarah 您是否在清单中授予此权限?
  • 是的,我放了一个字符串并将其分配给 message.getSubject() 但我每次都收到 null
  • @Sarah 您的收件箱有收到的主题邮件吗?如果没有,请尝试向该电子邮件地址发送带有主题的电子邮件。我在代码中没有看到任何问题。
  • @Sarah 顺便说一句,使用 pop3 协议,您的电子邮件将在您访问后从收件箱中删除。尝试改用 IMAP 协议。
猜你喜欢
  • 2021-08-18
  • 2015-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-27
  • 2014-10-08
  • 2018-07-28
相关资源
最近更新 更多