阅读Gmail邮件时出现Java ClassCastException

时间:2014-03-10 18:28:17

标签: java javamail

我想使用“imps”阅读我的gmail收件箱邮件。我正在使用此代码来阅读邮件。在这里,我获得了发件人的姓名,日期,主题和内容。请参阅以下代码:

import java.util.*;
import javax.mail.*;

public class ReadingEmail {
public static void main(String[] args) {
    Properties props = new Properties();
    props.setProperty("mail.store.protocol", "imaps");
    try {
        Session session = Session.getInstance(props, null);
        Store store = session.getStore();
        store.connect("imap.gmail.com", "mail", "pass");
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);
        Message msg = inbox.getMessage(inbox.getMessageCount());
        Address[] in = msg.getFrom();
        for (Address address : in) {
            System.out.println("FROM:" + address.toString());
        }
        Multipart mp = (Multipart) msg.getContent();
        BodyPart bp = mp.getBodyPart(0);
        System.out.println("SENT DATE:" + msg.getSentDate());
        System.out.println("SUBJECT:" + msg.getSubject());
        System.out.println("CONTENT:" + bp.getContent());
    } catch (Exception mex) {
        mex.printStackTrace();
    }
}
}

此代码在3-4天之前运行良好。现在我运行此代码,我收到此错误:

java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart
at pkg.ReadingEmail.main(ReadingEmail.java:21)

这是第21行的代码:

Multipart mp = (Multipart) msg.getContent();

请帮助我,以便我可以解决此异常 谢谢:))

0 个答案:

没有答案