Java: How to download a file via imap:// protocol? -
how can download file given following url java-se-7:
imap://georg@imap.acme.com:143/fetch%3euid%3e/inbox%3e18678?filename=testmail.eml
any highly appreciated - thank in advance.
here's basic solution. following code needs apache commons i/o 2.4 can downloaded here:
public class imapmessage { private string authority; private string protocol; private string host; private int port; private string username; private string password; private string foldername; private long msgid; private string filename; private message message; public imapmessage(string url) throws ioexception, messagingexception { parseurl(decodeurl(url)); } @override public string tostring() { return "protocol: "+protocol+"\n"+ "host: "+host+"\n"+ "port: "+port+"\n"+ "username: "+username+"\n"+ "password: "+password+"\n"+ "folder: "+foldername+"\n"+ "msgid: "+msgid+"\n"+ "filename: "+filename; } private string decodeurl(string url) throws ioexception { if(url!=null && !url.isempty()) { url = stringutils.replace(url, "%3e", ">"); url = stringutils.replace(url, "%20", " "); return url; } else { throw new ioexception("the given url empty or invalid."); } } private void parseurl(string url) throws ioexception, malformedurlexception { if(url!=null && !url.isempty()) { //<editor-fold defaultstate="collapsed" desc="parse protocol"> if(url.startswith("imaps")) { url = stringutils.replace(url, "imaps", "http", 1); protocol = "imaps"; } else if(url.startswith("imap")) { url = stringutils.replace(url, "imap", "http", 1); protocol = "imap"; } else { throw new ioexception("unsupported protocol: "+url.substring(0, url.indexof("://"))); } try { url newurl = new url(url); string path = newurl.getpath(); string query = newurl.getquery(); authority = newurl.getauthority(); host = newurl.gethost(); port = newurl.getport(); username = newurl.getuserinfo(); password = "provide password here"; foldername = path.substring(path.indexof(">/")+2, path.lastindexof(">")); msgid = long.parselong(path.substring(path.lastindexof(">")+1, path.length())); filename = query.substring(query.indexof("=")+1, query.length()); } catch (malformedurlexception ex) { throw ex; } } else { throw new ioexception("the given url empty or invalid."); } } public file fetchmessage() throws ioexception, filenotfoundexception, messagingexception { store store = null; folder folder = null; file filepath = new file("/destination/directory"); try { properties props = system.getproperties(); props.setproperty("mail.store.protocol", protocol); session session = session.getdefaultinstance(props, null); // session.setdebug(true); store = session.getstore(protocol); store.connect(host, port, username, password); folder = store.getfolder(foldername); folder.open(folder.read_only); uidfolder ufolder = (uidfolder)folder; message = ufolder.getmessagebyuid(msgid); if(message!=null) { file file = null; if(filename.equals("null")) { file = new file(filepath.getabsolutepath()+file.separator+long.tostring(system.nanotime())+".eml"); } else { file = new file(filepath.getabsolutepath()+file.separator+filename); } message.writeto(new fileoutputstream(file)); return file; } else { throw new messagingexception("the requested e-mail not found on mail server."); } } catch(exception ex) { throw ex; } { if(folder!=null) { folder.close(true); } if(store!=null) { store.close(); } } } }
Comments
Post a Comment