PackageCom.zz.mail; Importjava.io.FileNotFoundException; ImportJava.io.FileOutputStream; Importjava.io.IOException; ImportJava.io.InputStream; Importjava.io.UnsupportedEncodingException; Importjava.util.Properties; Importjavax.mail.Address; ImportJavax.mail.Folder; ImportJavax.mail.Message; Importjavax.mail.MessagingException; ImportJavax.mail.Multipart; Importjavax.mail.NoSuchProviderException; ImportJavax.mail.Part; Importjavax.mail.Session; ImportJavax.mail.Store; Importjavax.mail.internet.InternetAddress; Importjavax.mail.internet.MimeUtility; /*** e-mail Acceptance test **/ Public classPop3mailreceivertest { Publicpop3mailreceivertest () {Try { //1. Set the connection information to create a SessionString protocol = "POP3"; BooleanIsssl =true; String Host= "Pop.163.com"; intPort = 995; String username= "[Email protected]"; String Password= "****"; Properties Props=NewProperties (); Props.put ("Mail.pop3.ssl.enable", Isssl); Props.put ("Mail.pop3.host", host); Props.put ("Mail.pop3.port", Port); Session Session=session.getdefaultinstance (props); //2. Get the Store and connect to the serverStore store =session.getstore (protocol); Store.connect (username, password); Folder Folder= Store.getdefaultfolder ();//Default Parent Directory if(Folder = =NULL) {System.out.println ("Server Unavailable"); return; } /*System.out.println ("Default mailbox Name:" + folder.getname ()); folder[] folders = Folder.list ();//default directory list for (int i = 0; i < folders.length; i++) {SYSTEM.O Ut.println (Folders[0].getname ()); } System.out.println ("Number of subdirectories under the default directory:" + folders.length); */Folder Popfolder= Folder.getfolder ("INBOX");//Get InboxPopfolder.open (Folder.read_write);//readable mail, you can delete the message mode Open Directory//4. List all messages under inboxmessage[] Messages =popfolder.getmessages (); //number of messages taken out intMsgcount =Popfolder.getmessagecount (); System.out.println ("Total Mail:" + msgcount + "Seal"); //fetchprofile fprofile = new Fetchprofile ();//Select the download mode for the message,//Choose a different mode depending on the speed//Fprofile.add (FetchProfile.Item.ENVELOPE); //Folder.fetch (messages, fprofile);//selective download of messages//5. Cycle through each message and realize the ability to turn the message into news for(inti = 0; i < Msgcount; i++) { //Single MessageSystem.out.println ("First" + i + "mail Start"); Mailreceiver (Messages[i]); System.out.println ("First" + i + "Mail End"); //message reads are used to verifyMessages[i].writeto (NewFileOutputStream ("D:/pop3mailreceiver" + i + ". eml")); } //7. Close Folder will really delete the message, false does not deletePopfolder.close (true); //8. Close the store and disconnect the networkStore.close (); } Catch(nosuchproviderexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } /*** Parse Mail * *@paramMessages Mail Object *@paramI *@return * @throwsIOException *@throwsmessagingexception *@throwsFileNotFoundException *@throwsunsupportedencodingexception*/ Private voidMailreceiver (Message msg)throwsexception{//Sender Informationaddress[] Froms =Msg.getfrom (); if(Froms! =NULL) { //System.out.println ("Sender information:" + froms[0]); InternetAddress addr = (internetaddress) froms[0]; System.out.println ("Sender Address:" +addr.getaddress ()); System.out.println ("Sender Display Name:" +addr.getpersonal ()); } System.out.println ("Mail Subject:" +msg.getsubject ()); //getcontent () is the capture package content, part equivalent to the outer packagingObject o =msg.getcontent (); if(OinstanceofMultipart) {Multipart Multipart=(Multipart) o; Remultipart (multipart); } Else if(OinstanceofPart ) { Part part=(part) O; Repart (part); } Else{System.out.println ("Type" +Msg.getcontenttype ()); System.out.println ("Content" +msg.getcontent ()); } } /** * @paramPart parsing content *@throwsException*/ Private voidRepart (part part)throwsmessagingexception, Unsupportedencodingexception, IOException, FileNotFoundException {if(Part.getdisposition ()! =NULL) {String Strfilenmae= Mimeutility.decodetext (Part.getfilename ());//mimeutility.decodetext Troubleshooting Attachment name garbled problemSYSTEM.OUT.PRINTLN ("Found attachment:" +Mimeutility.decodetext (Part.getfilename ())); System.out.println ("Content Type:" +Mimeutility.decodetext (Part.getcontenttype ())); System.out.println ("Attachment content:" +part.getcontent ()); InputStream in= Part.getinputstream ();//open an input stream for an attachment//reads attachment bytes and stores them in a fileJava.io.FileOutputStream out =NewFileOutputStream (Strfilenmae); intdata; while(data = In.read ())! =-1) {out.write (data); } in.close (); Out.close (); } Else { if(Part.getcontenttype (). StartsWith ("Text/plain") {System.out.println ("Text content:" +part.getcontent ()); } Else { //System.out.println ("HTML content:" + part.getcontent ()); } } } /** * @parammultipart//unloading package (including all mail contents (Package + body + attachment)) *@throwsException*/ Private voidRemultipart (Multipart Multipart)throwsException {//System.out.println ("Mail Total" + multipart.getcount () + "part"); //process each part in turn for(intj = 0, n = multipart.getcount (); J < N; J + +) { //System.out.println ("Processing section" + j + "part"); Part part = Multipart.getbodypart (j);//unpack, take out the various parts of the multipart, each part may be the message content,//it could be another small package (Multippart)//judge if this package content is not a small package, generally this part is the body content-type:multipart/alternative if(Part.getcontent ()instanceofMultipart) {Multipart P= (Multipart) part.getcontent ();//turn into a small parcel//Recursive iterationsRemultipart (P); } Else{repart (part); } } } /** * @paramargs*/ Public Static voidMain (string[] args) {Newpop3mailreceivertest (); } }
Java Read mail