Bromon original, please respect the copyright
Second, the charge of the message
Usually we all use the POP3 protocol to collect mail, IMAP is now not involved. Although the function of collecting the mail is basically clear, but it will be basically included in So Easy.
Mail can be roughly divided into three: plain text messages, text messages containing other data, including messages with attachment.
Code / * * Created on 2004-4-26 * / package org.bromon.mail; import javax.mail. *; Import java.util. *; Import java.io. *;
/ ** * @Author bromon * / public class receiver {folder inbox; store store;
/ / Connect the mail server, get all mail list public message [] getmail (string host, string name, string password) throws exception {property = new profment (); prop.put ("mail.pop3.host", Host ); Session session = session.getDefaultinstance (prop); store = session.getStore ("POP3"); store.connect (Host, Name, Password); Inbox = store.getDefaultFolder (). GetFolder ("Inbox"); Inbox .open (Folder.READ_ONLY); Message [] msg = inbox.getMessages (); FetchProfile profile = new FetchProfile (); profile.add (FetchProfile.Item.ENVELOPE); inbox.fetch (msg, profile); return (msg }
// handle any kind of mail need to approach private void handle (Message msg) throws Exception {System.out.println ( "mail subject:" msg.getSubject ()); System.out.println ( "Mail Author: " msg.getFrom () [0] .tostring ()); system.out.println (" Send Date: " msg.getSentDate ());}
// Processing text mail Public void handletext (Message MSG) throws exception {this.handle (msg); system.out.println ("Mail content:" msg.getContent ());}
Multipart // process a message, including the public void handleMultipart function to save the attachment (Message msg) throws Exception {String disposition; BodyPart part; Multipart mp = (Multipart) msg.getContent (); int mpCount = mp.getCount (); / / Miltipart is used in except for multiple Parts, such as multiple attachments for (int m = 0; m ) {this.handle (msg); part = mp.getBodypart (m); disposition = part. GetDisPosition (); if (disposition! = null && disposition.equals (parttachment)) // Decisive whether there is attachment {//this.saveattach (PART) (// / / this method is responsible for saving attachments, comment) If there is a virus, please clean the mailbox and remove the comment} else {system.out.println (Part.getContent ());}}} private void savettach (bodypart part) throws exception {string temp = part.getFileName (); / / Get unprocessed attachment name string s = Temp.substring (11, Temp.indexof ("? =") - 1); // Go to Header and Footer // File Name General Take Base64 encoding, below is decoding String fileName = this.base64Decoder (s); System.out.println ( "have attachments:" fileName); InputStream in = part.getInputStream (); FileOutputStream writer = new FileOutputStream (new File (fileName)); byte [ ] Content = New Byte [255]; int = 0; while ((read = in.read) Content))! = - 1) {Writer.write (Content); }writer.close (); in. close ();
// base64 decoding private String base64Decoder (String s) throws Exception {sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder (); byte [] b = decoder.decodeBuffer (s); return (new String (b)) }
// Close Connect PUBLIC VOID Close () throws Exception {IF (Inbox! = Null) {INBOX.CLOSE (FALSE);} if (store! = Null) {store.close ();}}
Public static void main (string rgs []) {string host = "pop.163.com"; string name = "bromon"; string password = "my password"; Receiver Receiver = new receivediver (); try {message [] Msg = Receiver.getmail (Host, Name, Password); for (int i = 0; i