I am trying to download attachments from Hotmail messages and getting the following exception: com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 2 before EOF, the 2 most recent characters were: "JV". for every message with an attachment. I tried setting "mail.imaps.partialfetch" to "false" but it did not work, here is the code i'm using:
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();
}
}
here is the output i get if i call part.writeTo(System.out):
Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="pdf2.pdf"
JV