JavaMail附件使用Hotmail解码异常

时间:2015-10-26 18:17:19

标签: java javamail imap email-attachments hotmail

我正在尝试从Hotmail邮件中下载附件并获得以下异常:com.sun.mail.util.DecodingException:BASE64Decoder:编码流中的错误:需要4个有效的base64字符,但在EOF之前只有2个,最多2个最近的人物是:“JV”。对于每个带附件的邮件。我尝试将“mail.imaps.partialfetch”设置为“false”,但它不起作用,这是我正在使用的代码:

public class imapHotmail {


private static Object m;

public static void main(String[] args) {
    java.security.Security
            .addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    Properties props = new Properties();
    //System.setProperty("mail.mime.decodetext.strict", "false");
    //System.setProperty("mail.mime.base64.ignoreerrors", "true");
    System.setProperty("mail.imaps.partialfetch", "false");
    props.setProperty("mail.store.protocol", "imaps");
    props.setProperty("mail.imap.ssl.enable", "true");
    props.setProperty("mail.imaps.partialfetch", "false");

    try {

        Session session = Session.getInstance(props, null);
        // session.setDebug(true);
        Store store = session.getStore("imap");
        System.out.println("connecting....");
        store.connect("imap-mail.outlook.com", "soemone@hotmail.com", "pass");

        System.out.println("connected");

        Folder inbox = store.getFolder("inbox");
        inbox.open(Folder.READ_ONLY);
        Message[] msgs = inbox.getMessages();
        int x = 1;
        for (Message msg : msgs) {

            System.out.println(x);
            System.out.println(msg.getSubject());
            Object content = msg.getContent();
            if (content instanceof Multipart) {
                Multipart multipart = (Multipart) content;

                for (int i = 0; i < multipart.getCount(); i++) {
                    MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(i);

                    if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                        System.out.println("saving");
                        //part.getContent(); this only returns the parts headers and 2 chars from the encoded bodypart
                        String name = part.getFileName();
                        File f = new File("C:\\Users\\user\\Desktop\\" + name);
                        part.saveFile("C:\\Users\\user\\Desktop\\" + name);
                        System.out.println("saved");
                    }
                }
            }
            x++;
        }

        inbox.close(true);
        store.close();
    } catch (Exception mex) {
        mex.printStackTrace();

    }
}

这是我得到的输出,如果我调用part.writeTo(System.out):

Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="pdf2.pdf"

JV

0 个答案:

没有答案