java - cannot append a message to a folder in javax.mail -
i managing save simple messages containing body, subject etc. however, unable save multipart messages. logged before , after appendmessages
, noticed second log absent. interestingly, have no exceptions being fired @ all. have absolutely no idea going wrong here.
here java code:
store store = null; folder folder = null; string foldername = "sentbox"; try { session session = preparesession(mailprotocols.imap, kid); store = session.getstore("imap"); store.connect(myhost, user.getlogin(), user.getpassword()); folder = store.getfolder(foldername); if (folder == null || !folder.exists()) { folder.create(folder.holds_messages); } folder.open(folder.read_write); mimemessage mimemessage = new mimemessage(session); address[] = null; if(msg.getto() != null) { // msg instance of custom message class, nothing special there int msgsize = msg.getto().size(); = new internetaddress[msgsize]; (int = 0; < msgsize; i++) { to[i] = new internetaddress(msg.getto().get(i)); } } mimemessage.setrecipients(recipienttype.to, to); mimemessage.setsentdate(new date(system.currenttimemillis())); mimemessage.setsubject(msg.getsubject()); if (msg.getfiles() != null) { mimemultipart mp = new mimemultipart(); mimebodypart newpart = new mimebodypart(); newpart.settext(msg.getbody()); mp.addbodypart(newpart); (multipartfile multipartfile : msg.getfiles()) { try { newpart = new mimebodypart(); // create new part each files newpart.addheader("my-file-type", multipartfile.getcontenttype()); file tmpfile = file.createtempfile("newattachment", ".tmp"); multipartfile.transferto(tmpfile); filedatasource fds = new filedatasource(tmpfile); newpart.setdatahandler(new datahandler(fds)); newpart.setfilename(multipartfile.getoriginalfilename()); newpart.setdisposition(part.attachment); mp.addbodypart(newpart); tmpfile.deleteonexit(); } catch (ioexception e) { logger.debug("can not create temp file ===========>"); e.printstacktrace(); } } mimemessage.setcontent(mp); mimemessage.savechanges(); } else { mimemessage.settext(msg.getbody()); } folder.appendmessages(new message[] {mimemessage}); message[] allmessages = folder.getmessages(); uidfolder uidfolder = (uidfolder) folder; long savedmsgid = uidfolder.getuid(allmessages[allmessages.length - 1]); logger.info("savedmsgid",savedmsgid + "") //cannot output @ } catch (exception e) { logger.error(e); } { closemailstore(store, folder); // simple method closes store }
i using apache james 3.0.4. approaches welcome
i using apache james 3.0.4
don't mean apach james 3.0-beta4? i.e. haven't yet reached 3.0 release, there no 3.0.4. reason you're not using stable release (2.3.2)? asking... :-)
i logged before , after appendmessages, noticed second log absent.
if never logging code after folder.appendmessages(new message[] {mimemessage});, there 3 possibilities:
- an error (or throwable) being thrown inside javax.mail / io code.
examples include ioerror (nasty io interfacing failure), linkageerror (incompatible jars/classes) or codermalfunctionerror (thrown charsetdecoder or charsetencoder if decoding/encoding loop throws unexpected exception). - thread problems cause program reach standstill - i.e. thread starvation or deadlock
- jvm process crash
item (1) by far probable here. suggestion: change tail-end of code to:
} catch (exception e) { logger.error(e); } catch (throwable t) { logger.error(t); } { closemailstore(store, folder); // simple method closes store }
if logs throwable, can investigate cause in terms of app jars & configuration, os/jvm version, or data content...
if doesn't log throwable, can investigate (2) or (3).
Comments
Post a Comment