The following is a tool class for mail reception, a bit long!!!
public class Recivemail {private mimemessage msg = null;
Private String Saveattchpath = "";
Private StringBuffer BodyText = new StringBuffer ();
Private String dateformate = "Yy-mm-dd hh:mm";
Public Recivemail (MimeMessage msg) {this.msg = msg;
public void Setmsg (MimeMessage msg) {this.msg = msg; /** * Get Sender information * @return * @throws messagingexception/public String Getfrom () throws Messag
ingexception{internetaddress[] address = (internetaddress[]) msg.getfrom ();
String from = Address[0].getaddress ();
if (from = = null) {from = "";
String personal = address[0].getpersonal ();
if (personal = = null) {personal = "";
String fromaddr = personal + "<" +from+ ">";
return fromaddr; /** * Get mail Recipient, CC, BCC address and information.
According to the parameters passed are different "to"--> recipients, CC--> cc address, bcc--> address * @param type * @return * @throws messagingexception * @throws UnsupporTedencodingexception */public string getmailaddress (String type) throws Messagingexception, Unsupportedencodingexc
eption{String mailaddr = "";
String addrtype = Type.touppercase ();
internetaddress[] address = null; if (Addrtype.equals ("to") | | Addrtype.equals ("CC") | | Addrtype.equals ("BCC")) {if (Addrtype.equals ("to")) {address = (internetaddress[]) msg.getrecipients (messag
E.RECIPIENTTYPE.TO);
} if (Addrtype.equals ("CC")) {address = (internetaddress[]) msg.getrecipients (Message.RecipientType.CC); } if (Addrtype.equals ("BCC")) {address = (internetaddress[]) msg.getrecipients (message.recipienttype.
BCC); } if (address!= null) {for (int i=0;i<address.length;i++) {String mail = Address[i].getadd
Ress ();
if (mail = = null) {mail = "";
}else{mail = mimeutility.decodetext (mail); } String personal = addRess[i].getpersonal ();
if (personal = = null) {personal = "";
}else{personal = mimeutility.decodetext (personal);
String Compositeto = personal + "<" +mail+ ">";
Mailaddr + = "," +compositeto;
} mailaddr = mailaddr.substring (1);
}}else{throw new RuntimeException ("Error email type!");
return mailaddr;
/** * Get mail subject * @return * @throws unsupportedencodingexception * @throws messagingexception * *
public string Getsubject () throws Unsupportedencodingexception, messagingexception{string subject = "";
Subject = Mimeutility.decodetext (Msg.getsubject ());
if (subject = = null) {subject = "";
return subject; /** * Get mail Send date * @return * @throws messagingexception/public String getsenddate () throws Mes
sagingexception{Date senddate = Msg.getsentdate (); SimpledaTeformat SMD = new SimpleDateFormat (dateformate);
Return Smd.format (senddate);
/** * Get message body content * @return/public String Getbodytext () {return bodytext.tostring (); /** * parsing mail, the resulting message content is saved to a StringBuffer object, * parsing the message mainly according to mimetype different operations, step-by-Step Analysis * @param part * @th Rows messagingexception * @throws IOException */public void Getmailcontent (part part) throws Messagingexceptio
N, ioexception{String contentType = Part.getcontenttype ();
int nameindex = contenttype.indexof ("name");
Boolean conname = false;
if (Nameindex!=-1) {conname = true;
} System.out.println ("CONTENTTYPE:" +contenttype);
if (Part.ismimetype ("Text/plain") &&!conname) {bodytext.append ((String) part.getcontent ());
}else if (Part.ismimetype ("text/html") &&!conname) {bodytext.append ((String) part.getcontent ()); }else if (Part.ismimetype ("multipart/*")) {multipart MultipaRT = (Multipart) part.getcontent ();
int count = Multipart.getcount ();
for (int i=0;i<count;i++) {getmailcontent (Multipart.getbodypart (i));
}}else if (Part.ismimetype ("message/rfc822")) {Getmailcontent ((part) part.getcontent ()); }/** * Determines whether a message requires a receipt, returns true if a receipt is returned, false * @return * @throws messagingexception/Publi
C Boolean getreplysign () throws messagingexception{Boolean replysign = false;
String needreply[] = Msg.getheader ("disposition-notification-to");
if (needreply!= null) {replysign = true;
return replysign; /** * Get this message Message-id * @return * @throws messagingexception/public String Getmessageid () t
Hrows messagingexception{return Msg.getmessageid (); /** * Determines if this message is read, returns False if unread, read returns True * @return * @throws messagingexception/public Boolean IsNew () throws messagingexception{Boolean isnew= false;
Flags flags = ((message) msg). GetFlags ();
flags.flag[] Flag = Flags.getsystemflags ();
SYSTEM.OUT.PRINTLN ("Flags ' s Length:" +flag.length);
for (int i=0;i<flag.length;i++) {if (flag[i]==flags.flag.seen) {isnew = true;
SYSTEM.OUT.PRINTLN ("seen message ...");
Break
} return isnew;
/** * Determine if the attachment is included * @param part * @return * @throws messagingexception * @throws IOException
* * Public boolean Iscontainattch (part part) throws Messagingexception, ioexception{boolean flag = false;
String ContentType = Part.getcontenttype ();
if (Part.ismimetype ("multipart/*")) {multipart multipart = (multipart) part.getcontent ();
int count = Multipart.getcount ();
for (int i=0;i<count;i++) {BodyPart bodypart = Multipart.getbodypart (i);
String dispostion = Bodypart.getdisposition (); if (dispostion!= null) && (dispostiOn.equals (part.attachment) | |
Dispostion.equals (Part.inline))) {flag = true;
}else if (Bodypart.ismimetype ("multipart/*")) {flag = Iscontainattch (bodypart);
}else{String contype = Bodypart.getcontenttype ();
if (Contype.tolowercase (). IndexOf ("appliaction")!=-1) {flag = true;
} if (Contype.tolowercase (). IndexOf ("name")!=-1) {flag = true; }}}else if (Part.ismimetype ("message/rfc822")) {flag = Iscontainattch ((part) Part.getconten
T ());
return flag; /** * Save Attachment * @param part * @throws messagingexception * @throws ioexception/public void
Saveattchment (part part) throws Messagingexception, ioexception{String filename = "";
if (Part.ismimetype ("multipart/*")) {Multipart MP = (multipart) part.getcontent (); for (int i=0;i<mp.getcount (); i++) {BodyPart Mpart = Mp.getBodyPart (i);
String dispostion = Mpart.getdisposition (); if (dispostion!= null) && (dispostion.equals (part.attachment) | |
Dispostion.equals (part.inline)) {filename = Mpart.getfilename ();
if (Filename.tolowercase (). IndexOf ("gb2312")!=-1) {filename = mimeutility.decodetext (filename);
} savefile (Filename,mpart.getinputstream ());
}else if (Mpart.ismimetype ("multipart/*")) {saveattchment (Mpart);
}else{filename = Mpart.getfilename (); if (filename!= null&& (filename.tolowercase (). IndexOf ("gb2312")!=-1)) {filename = Mimeutility.decodete
XT (filename);
} savefile (Filename,mpart.getinputstream ());
}}else if (Part.ismimetype ("message/rfc822")) {Saveattchment ((part) part.getcontent ()); /** * Gets the address of the save attachment * @return/Public String Getsaveattchpath () {return savEattchpath; /** * Settings Save attachment Address * @param saveattchpath */public void Setsaveattchpath (String saveattchpath) {thi
S.saveattchpath = Saveattchpath; /** * Set Date format * @param dateformate */public void setdateformate (String dateformate) {This.datefo
Rmate = dateformate; /** * Save File Contents * @param filename * @param inputstream * @throws ioexception/private void Save
File (string filename, InputStream inputstream) throws IOException {string osname = System.getproperty ("Os.name");
String Storedir = Getsaveattchpath ();
String sepatror = "";
if (Osname = = null) {osname = "";
} if (Osname.tolowercase (). IndexOf ("Win")!=-1) {sepatror = "//"; if (storedir==null| | "".
Equals (Storedir)) {Storedir = "d://temp";
}}else{Sepatror = "/";
Storedir = "/temp";
} File StoreFile = new file (storedir+sepatror+filename); System.out. println ("StoreFile ' s Path:" +storefile.tostring ());
Bufferedoutputstream BOS = NULL;
Bufferedinputstream bis = null;
try {bos = new Bufferedoutputstream (new FileOutputStream (StoreFile));
bis = new Bufferedinputstream (InputStream);
int C;
while ((c= bis.read ())!=-1) {bos.write (c);
Bos.flush ();
The catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}finally{Bos.close ();
Bis.close (); } public void Recive (part part,int i) throws Messagingexception, ioexception{System.out.println ("---
---------------START-----------------------");
SYSTEM.OUT.PRINTLN ("message" +i+ "Subject:" + getsubject ());
SYSTEM.OUT.PRINTLN ("message" +i+ "from:" + getfrom ());
SYSTEM.OUT.PRINTLN ("message" +i+ "isnew:" + isnew ()); Boolean flag = Iscontainattch (part);
SYSTEM.OUT.PRINTLN ("message" +i+ "Iscontainattch:" +flag);
SYSTEM.OUT.PRINTLN ("message" +i+ "replysign:" + getreplysign ());
Getmailcontent (part);
SYSTEM.OUT.PRINTLN ("message" +i+ "content:" + getbodytext ());
Setsaveattchpath ("c://temp//" +i);
if (flag) {saveattchment (part);
} System.out.println ("------------------end-----------------------");
}
}
The use of mail reception and tool class, there are several types of mail received the writing! :
Read a lot of other online code, Pop3server value is pop.mail.163.com, but try unsuccessfully, there are user values can be either with username@...com or username.
If the Inbox is 163 mailbox, must first log in 163 mailbox settings, open POP3 services. As for the other mailboxes are not known.
public static void Main (string[] args) throws Exception {//Connection POP3 server host name, protocol, user name, password String pop3server = "POP.1
63.com ";
String protocol = "POP3";
String user = "username";
String pwd = "password";
Create a Properties object with specific connection information properties props = new properties ();
Props.setproperty ("Mail.store.protocol", protocol);
Props.setproperty ("Mail.pop3.host", pop3server);
Use Properties object to get Session object Session session = Session.getinstance (props);
Session.setdebug (TRUE);
Use the Session object to get the store object and connect to the POP3 server store store = Session.getstore ();
Store.connect (pop3server, user, PWD);
Get the Mail folder folder object in the mailbox, open Folder folder = Store.getfolder ("Inbox") with "Read Only";
Folder.open (folder.read_only);
Get all message messages object message [] messages = Folder.getmessages () in folder of mail folder;
Recivemail rm = null; for (int i=0;i< messages.size (); i++) {rm = new Recivemail ((mimemessage) messages[I]);
Rm.recive (Messages[i],i);
} folder.close (FALSE);
Store.close ();
}