接收邮件javax.mail.internet.MimeMultipart@268d15时出错

时间:2015-09-04 03:33:26

标签: java javamail

您好我一直致力于接收电子邮件的应用程序。我必须显示主题,身体,附件。问题是当邮件中有可用附件时,则不会显示邮件内容。它显示为javax.mail.internet.MimeMultipart@268d15。我搜索了很多网站,但没有得到解决方案。请帮助我..

我的代码是

import java.util.Properties;
import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;

public class EmailAttachReceiver {

String fileName;
public void downloadEmailAttachments(String userName, String password) {

    try {

        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imaps");
        Session session = Session.getInstance(props, null);
        Store store = session.getStore();
        store.connect("imap.gmail.com", userName, password);
        Folder folderInbox = store.getFolder("INBOX");
        folderInbox.open(Folder.READ_ONLY);
        Message[] arrayMessages = folderInbox.getMessages();
        for (int i = 0; i < arrayMessages.length; i++) {
            //i=arrayMessages.length-1;
            Message message = arrayMessages[i];
            Address[] fromAddress = message.getFrom();
            String from = fromAddress[0].toString();
            String subject = message.getSubject();
            String sentDate = message.getSentDate().toString();
            String contentType = message.getContentType();
            String messageContent = "";
            String attachFiles = "";

            if (contentType.contains("multipart")) {
                //System.out.println("contentType 1 "+contentType);
                Multipart multiPart = (Multipart) message.getContent();
                int numberOfParts = multiPart.getCount();
                //System.out.println("numberOfParts 1 "+numberOfParts);
                for (int partCount = 0; partCount < numberOfParts; partCount++) {
                    MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
                    if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                      //  System.out.println("partCount 1 " + partCount);
                       fileName  = part.getFileName();
                    } else {
                        //  System.out.println("partCount 2 "+partCount);
                        messageContent = part.getContent().toString();
                    }
                }

            } else if (contentType.contains("text/plain")
                    || contentType.contains("text/html")) {
                //System.out.println("contentType 2 " + contentType);
                Object content = message.getContent();
                if (content != null) {
                    messageContent = content.toString();
                }
            }

            System.out.println("Message #" + (i + 1) + ":");
            System.out.println("\t From: " + from);
            System.out.println("\t Subject: " + subject);
            System.out.println("\t Sent Date: " + sentDate);
            System.out.println("\t Message: " + messageContent);
            System.out.println("\t Attachments: " + fileName);
        }

        folderInbox.close(false);
        store.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public static void main(String[] args) {

    String userName = "";
    String password = "";
    EmailAttachReceiver receiver = new EmailAttachReceiver();
    receiver.downloadEmailAttachments(userName, password);

}

}

1 个答案:

答案 0 :(得分:0)

示例消息的结构失败了什么?你是否可能遇到一个多部分嵌套在多部分中的消息? JavaMail msgshow.java sample program可以显示消息的结构。另请参阅this JavaMail FAQ entry for finding the main body part in a message