在邮件JAVA中读取错误

时间:2017-07-27 13:35:28

标签: java javamail

我正在阅读来自gmail.com的电子邮件。我已经成功阅读了一些邮件,但过了一段时间我得到了这个,

 java.lang.ClassCastException: 
 javax.mail.internet.MimeMultipart cannot be cast to java.lang.String
 at emailIngestion.EmailIngestion.check(EmailIngestion.java:66)
 at emailIngestion.EmailIngestion.main(EmailIngestion.java:106).

实际上我的要求是将内容存储到变量中,然后将其存储在arraylist中,然后再将其写入文件...我已经使用此代码实现了它,所以如果有更好的想法请分享我。

public class EmailIngestion {   
static ArrayList<EmailModel> contentList=new ArrayList<EmailModel>();
static ArrayList<EmailModel> metaDataList= new ArrayList<EmailModel>();
public static void check(String host, String storeType, String user,
        String password) throws IOException 
{
    FileWriter fw= new FileWriter("C:\\Users\\Admin\\Desktop\\murtaza_metadata.csv",true);
    FileWriter fw1= new FileWriter("C:\\Users\\Admin\\Desktop\\murtaza_content.txt",true);
    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);
        Store store = emailSession.getStore("pop3s");
        store.connect(host, user, password);
        Folder emailFolder = store.getFolder("INBOX");
        emailFolder.open(Folder.READ_ONLY);
        Message[] messages = emailFolder.getMessages();
        int emailNumber;
        String mailContent = null,from = null,to = null,mailContentType = null,subject = null;
        Date recievedDate= new Date(); 
        System.out.println("messages.length---" + messages.length);
        for (int i = 0, n = messages.length; i < n; i++) {
            Message message = messages[i];
            System.out.println("Subject is"+message.getSubject());
            subject = message.getSubject();
            from =  message.getFrom()[0].toString();
            mailContentType=message.getContentType();
            recievedDate=message.getSentDate();
            to=InternetAddress.toString(message.getRecipients(Message.RecipientType.TO));
            emailNumber=message.getMessageNumber();
            metaDataList.add(new EmailModel(from, to, subject, mailContentType, recievedDate,emailNumber));
            fw.write(emailNumber+"\001"+from+"\001"+subject+"\001"+recievedDate+mailContentType+"\001"+" \n");
            fw.flush();
            if(message.isMimeType("multipart/*")){
                Multipart multipart = (Multipart) message.getContent(); 
                for (int x = 0; x < multipart.getCount(); x++) {
                    BodyPart bodyPart = multipart.getBodyPart(x);
                        mailContent=(String) bodyPart.getContent();
                        System.out.println(mailContent);
                    }
                }
            else{
                mailContent=(String) message.getContent();
                System.out.println(message.getContent());
            }
            contentList.add(new EmailModel(mailContent, emailNumber));
            fw1.write(emailNumber+","+mailContent+"\n");
        }
        emailFolder.close(false);
        store.close();

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

public static void main(String[] args) throws IOException {

    String host = "pop.gmail.com";// change accordingly
    String mailStoreType = "pop3";
    String username = "abcd@gmail.com";// change accordingly
    String password = "******";// change accordingly
    check(host, mailStoreType, username, password);

}

}

提前致谢

1 个答案:

答案 0 :(得分:0)

如果服务波动,那么它肯定不稳定。您使用的是哪个版本的1.3?在使用一个节点时,请确保已安装并运行主服务和工作服务。您可以从集群管理器中看到这一点。来自日志的信息不足以解决问题。