message.getContent返回null,尽管能够正确获取收件箱中的邮件数量

时间:2018-03-29 17:16:38

标签: android javamail

我使用JavaMail API在我的Android应用中阅读电子邮件。我调试了我的应用,发现数组中Message个对象的数量等于我收件箱中的电子邮件数量,但我在尝试使用{null时总是getContent 1 {}在message对象上。我使用了here找到的罐子。我正在使用imap.gmail.com以及所有正确的细节。我甚至尝试了一个名为mail.com的不同电子邮件服务,但错误仍然存​​在。我只提供用于阅读电子邮件的方法。你可以假设其他一切都是正确的。

代码: -

private void initMail(String hostval, String mailStrProt, String uname, String pwd, String authEmail, int mode)
{

    uriArrayList = new ArrayList<>();

    try {

        Log.d("LOGCAT","initMail");
        //Set property values
        Properties propvals = new Properties();
        propvals.put("mail.store.protocol", mailStrProt);
        propvals.put("mail.imap.user", uname);
        propvals.put("mail.imap.ssl.enable", "true");
        propvals.put("mail.imap.host", hostval);
        propvals.put("mail.imap.port", "993");
        propvals.put("mail.imap.starttls.enable", "true");
        Session emailSessionObj = Session.getInstance(propvals, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(uname, pwd);
            }
        });
        //Create POP3 store object and connect with the server
        Store storeObj = emailSessionObj.getStore(mailStrProt);
        storeObj.connect(hostval, uname, pwd);
        //Create folder object and open it in read-only mode
        Folder emailFolderObj = storeObj.getFolder("INBOX");
        emailFolderObj.open(Folder.READ_ONLY);
        //Fetch messages from the folder and print in a loop
        Message[] messageobjs = emailFolderObj.getMessages();

        for (Message message : messageobjs) {

            if(message.getFrom()[0].toString().equals(authEmail)) {

                if(message.getContent() instanceof String) { Log.d("LOGCAT","String"); uriArrayList.add(stringToUri(message.getContent().toString())); }
                else if(message.getContent() instanceof Multipart) {

                    Multipart multipart = (Multipart) message.getContent();
                    MimeBodyPart mimeBodyPart;
                    for(int i = 0 ; i < multipart.getCount() ; i++) {

                        if(MimeBodyPart.ATTACHMENT.equalsIgnoreCase(multipart.getBodyPart(i).getDisposition())) {

                            mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(i);
                            File file = new File(mimeBodyPart.getFileName());
                            mimeBodyPart.saveFile(file);
                            uriArrayList.add(Uri.fromFile(file));

                        }

                    }

                }

                lastReceivedMailDate = message.getSentDate().toString();

            }

        }
        //Now close all the objects
        emailFolderObj.close(false);
        storeObj.close();
    } catch (NoSuchProviderException exp) {
        exp.printStackTrace();
    } catch (MessagingException exp) {
        exp.printStackTrace();
    } catch (Exception exp) {
        exp.printStackTrace();
    }

}

编辑:

for (Message message : messageobjs) {

            Multipart multipart = (Multipart) message.getContent();
            MimeBodyPart mimeBodyPart;
            for(int i = 0 ; i < multipart.getCount() ; i++) {

                    mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(i);
                    File file = new File(mimeBodyPart.getFileName()); //line number 204
                    mimeBodyPart.saveFile(file); 
                    uriArrayList.add(Uri.fromFile(file));



            } }

0 个答案:

没有答案