java - Missing start boundary Exception when reading messages with an attachment file -
i don't know why i'm getting following exception when reading mail attachment file mail server:
exception in thread "main" javax.mail.messagingexception: missing start boundary @ javax.mail.internet.mimemultipart.parsebm<mimemultipart.java:872) @ javax.mail.internet.mimemultipart.parse<mimemultipart.java:493) @ javax.mail.internet.mimemultipart.getcount<mimemultipart.java:240) @ getparts.handlemultipart(getparts.java:57) @ getparts.main(getparts.java:42)
the file i'm using read messages is:
import java.io.*; import java.util.properties; import javax.mail.*; import javax.mail.internet.*; public class getparts { public static void main (string args[]) throws exception { string host = args[0]; string username = args[1]; string password = args[2]; // session properties props=new properties(); props.put("mail.mime.multipart.ignoremissingboundaryparamete",true); session session = session.getinstance( props, null); contenttype ct=new contenttype(); // store store store = session.getstore("pop3"); store.connect(host, username, password); // folder folder folder = store.getfolder("inbox"); folder.open(folder.read_only); bufferedreader reader = new bufferedreader ( new inputstreamreader(system.in)); // directory message message[] = folder.getmessages(); (int i=0, n=message.length; i<n; i++) { system.out.println(i + ": " + message[i].getfrom()[0] + "\t" + message[i].getsubject()); //message[i].setheader("content-type","multipart/mixed"); system.out.println("do want content? [yes read/quit end]"); string line = reader.readline(); if ("yes".equals(line)) { object content = message[i].getcontent(); if (content instanceof multipart) { handlemultipart((multipart)content); } else { handlepart(message[i]); } } else if ("quit".equals(line)) { break; } } // close connection folder.close(false); store.close(); } public static void handlemultipart(multipart multipart) throws messagingexception, ioexception { system.out.println(multipart.getcount()); (int i=0, n=multipart.getcount(); i<n; i++) { handlepart(multipart.getbodypart(i)); } } public static void handlepart(part part) throws messagingexception, ioexception { string disposition = part.getdisposition(); system.out.println("disposition "+disposition); string contenttype = part.getcontenttype(); system.out.println("contenttype "+contenttype); if (disposition == null) { // when body system.out.println("null: " + contenttype); // check if plain if ((contenttype.length() >= 10) && (contenttype.tolowercase().substring( 0, 10).equals("text/plain"))) { part.writeto(system.out); } else { // don't think happen system.out.println("other body: " + contenttype); part.writeto(system.out); } } else if (disposition.equalsignorecase(part.attachment)) { system.out.println("attachment: " + part.getfilename() + " : " + contenttype); savefile(part.getfilename(), part.getinputstream()); } else if (disposition.equalsignorecase(part.inline)) { system.out.println("inline: " + part.getfilename() + " : " + contenttype); savefile(part.getfilename(), part.getinputstream()); } else { // should never happen system.out.println("other: " + disposition); } } public static void savefile(string filename, inputstream input) throws ioexception { if (filename == null) { filename = file.createtempfile("xx", ".out").getname(); } // no overwrite existing file file file = new file(filename); (int i=0; file.exists(); i++) { file = new file(filename+i); } fileoutputstream fos = new fileoutputstream(file); bufferedoutputstream bos = new bufferedoutputstream(fos); bufferedinputstream bis = new bufferedinputstream(input); int abyte; while ((abyte = bis.read()) != -1) { bos.write(abyte); } bos.flush(); bos.close(); bis.close(); } }
i've had same problem. boundary specified within multipart content-type. can find further information in source. can watch 1 of current message using getcontenttype()
function. in case obtained result:
multipart/mixed; boundary=--boundary_25_2d74d02b-d0d6-4f28-a311-4d1b7d107417
so getcount()
function uses boundary separate parts compose multiple part. looks there cases in boundary corrupted.
the mail.mime.multipart.ignoreexistingboundaryparameter system property may set true cause boundary ignored , instead search boundary line in message mail.mime.multipart.ignoremissingboundaryparameter.
i followed instructions , works right. added code below:
system.setproperty("mail.mime.multipart.ignoreexistingboundaryparameter", "true");
hope helps!
Comments
Post a Comment