Mail receiving program written by JavaMail

xiaoxiao2021-03-06  70

Package coffeecatwebmail;

Import java.io. *;

Import java.text. *;

Import java.util. *;

Import javax.mail. *;

Import javax.mail.internet. *;

public

Class PrasemimeMimeMessage {

PRIVATE MIMEMESSAGEMEMESSAGE = NULL;

Private string saveattachpath =

""

// Storage directory after attachment download

Private stringbuffer bodytext =

NEW stringbuffer ();

/ / StilingBuffer object to store mail content

Private string DateFormat =

"YY-MM-DD HH: MM";

// Default reception format

/ ** * Constructor, initialize a MIMEMESSAGE object * /

Public PrasemimeMestage () {}

Public PRASEMIMEMEMEMEME (MIMEMESSAGE MIMEMESSAGE) {

this.mimeMimeMessage = mimeMessage;

System.out.println (

"CREATE A PRASEMEMESSAGE Object ........");

}

public

Void setMimeMessage (MIMEMESSAGE MIMEMESSAGE) {

this.mimeMimeMessage = mimeMessage;

}

/ ** * get the address and name of the sender * /

Public string getFrom ()

Throws exception {

InternetAddress address [] = (InternetDress []) mimeMessage.getFrom ();

String from = address [0] .Getaddress ();

IF (from == NULL) from =

""

String person = address [0] .getPersonal ();

IF (personal == null) Personal =

""

String fromAddr = Personal

"<" from

">"

Return fromaddr;

}

/ ** * Get the recipient of the message, the address and name of the sender, according to the different * "to" according to the parameters passed ---- Recipient "CC" --- Cc "Address "BCC" --- Delivery person Address * /

Public String getMailAddress (String Type)

Throws exception {

String mailaddr =

""

String addtype = type.touppercase ();

InternetAddress [] Address = NULL;

IF (AddType.equals

"To") || addtype.equals

"Cc") || addtype.equals

"BCC")) {

IF (AddType.equals

"To")) {

Address = (InternetAddress []) mimeMessage.getRecipients (Message.RecipientType.to);

}

Elseif (AddType.equals

"Cc")) {

Address = (InternetAddress []) mimeMessage.getRecipients (Message.RecipientType.cc);

}

Else {

Address = (InternetDress []) MimeMessage.GetRecipients (Message.RecipientType.bcc);

}

IF (address! = null) {

FOR

INT i = 0; i

String email = address [i] .getaddress ();

IF (email == null) Email =

""

Else {

Email = mimeutility.decodetext (email);

}

String personal = address [i] .GetPersonal ();

IF (personal == null) Personal =

""

Else {

Personal = mimeutility.decodeText (Personal);

}

String CompositEto = Personal

"<" email

">"

Mailaddr =

"," CompositiveTo;

}

Mailaddr = mailaddr.substring (1);

}

}

Else {

Throw

New Exception

"Error Emailaddr Type!");

}

Return mailaddr;

}

/ ** * get mail topic * /

Public string getSubject ()

THROWS MESSAGINGEXCEPTION {

String subject =

""

Try {

Subject = mimeutility.decodetext (mimeMessage.getsubject ());

IF (Subject == Null) Subject =

""

}

Catch (Exception Exce) {

}

Return Subject;

}

/ ** * Get email delivery date * /

Public string getsentdate ()

Throws exception {

Date SentDate = mimeMessage.getSentdate ();

SimpleDateFormat Format =

New SimpleDateFormat (DateFormat);

Return Format.Format (SENTDATE);

}

/ ** * Get the text content of the message * /

Public string getBodytext () {

Return Bodytext.toString ();

}

/ ** * Resolve the message, save the obtained message content into a StringBuffer object, parsing the message * mainly according to the different operations of the MIMETYPE type, one step by step is resolved * /

public

Void getMailContent (Part part)

Throws exception {

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 counts = multipart.getcount ();

FOR

INT i = 0; i

GetMailContent (Multipart.getBodypart (i));

}

}

Else

IF (Part.ismimeType

"Message / RFC822")) {

GetmailContent ((part) part.getContent ());

}

Else {}

}

/ ** * Determine if this message needs to be reached, if you need a receipt to return "true", otherwise return "false" * /

public

Boolean getReplySign ()

THROWS MESSAGINGEXCEPTION {

Boolean replySign =

False;

String needreply [] = mimeMessage.getHeader

"Disposition-notification-to");

IF (NeedrePly! = NULL) {

ReplySign =

True;

}

Return replysign;

}

/ ** * Message-id * / for this message

Public String getMessageId ()

THROWS MESSAGINGEXCEPTION {

Return MimeMessage.getMessageId ();

}

/ ** * [Determine if this message is read, if not read, return false, turn back to true] * /

public

Boolean isnew ()

THROWS MESSAGINGEXCEPTION {

Boolean isnew =

False;

Flags flags = ((message) mimeMessage) .getflags ();

Flags.flag [] flag = flags.getsystemflags ();

System.out.println (

"Flags's Length:" Flag.Length);

FOR

INT i = 0; i

IF (Flag [I] == Flags.Flag.seen) {

Isnew =

True;

System.out.println (

"Seen Message .......");

Break;

}

}

Return isnew;

}

/ ** * Judging whether this message contains attachments * /

public

Boolean iscontainattach (part part) throws exception {

Boolean attachflag =

False;

String contenttype = part.getContentType ();

IF (Part.ismimeType

"MULTIPART / *")) {

Multipart MP = (Multipart) Part.getContent ();

FOR

INT i = 0; i

Bodypart mpart = mp.getBodypart (i);

String disposition = mpart.getdisposition ();

IF ((Disposition! = null) && ((disposition.equals (part.attachment)) || (disposition.equals (part.inline)))))))))))

attachflag =

True;

Else

IF (Mpart.ismenstype

"MULTIPART / *")) {

Attachflag = iscontainattach ((part) mPart);

}

Else {

String lastpe = mpart.getContentType ();

IF (conty.tolowercase (). Indexof

"application")! = -1) attachflag =

True;

IF (conty.tolowercase (). Indexof

"Name")! = -1) attachflag =

True;

}

}

}

Else

IF (Part.ismimeType

"Message / RFC822")) {

Attachflag = iscontainattach ((part) part.getContent ());

}

Return attachflag;

}

/ ** * [save attachment] * /

public

Void SavetTachment (Part part)

Throws exception {

String filename =

""

IF (Part.ismimeType

"MULTIPART / *")) {

Multipart MP = (Multipart) Part.getContent ();

FOR

INT i = 0; i

Bodypart mpart = mp.getBodypart (i);

String disposition = mpart.getdisposition ();

IF ((Disposition! = null) && ((Disposition.Equals (part.attachment)) || (part.equals (part.inline)))))

Filename = mpart.getFileName ();

IF (filename.tolowercase (). Indexof

"GB2312")! = -1) {

Filename = mimeutility.decodetext (filename);

}

Savefile (filename, mpart.getInputstream ());

}

Else

IF (Mpart.ismenstype

"MULTIPART / *")) {

SavetTachment (MPART);

Else {

Filename = mpart.getFileName ();

IF (FileName! = null) && (filename.tolowercase (). Indexof

"GB2312")! = -1)) {

Filename = mimeutility.decodetext (filename);

Savefile (filename, mpart.getInputstream ());

}

}

}

}

Else

IF (Part.ismimeType

"Message / RFC822")) {

Savettachment ((part) part.getContent ());

}

}

/ ** * [Set attachment storage path] * /

public

Void settachpath (string attachpath) {

this.saveattachpath = attachpath;

}

/ ** * [Set date display format] * /

public

Void setDateFormat (String Format)

Throws exception {

this.dateFormat = Format;

}

/ ** * [Get an attachment storage path] * /

Public string getattachpath () {

Return saveattachpath;

}

/ ** * [Real saved attachment to the specified directory] * /

Private

Void Savefile (String FileName, InputStream in)

Throws exception {

String OsName = System.getProperty

"os.name");

String storedir = getattachpath ();

String separator =

""

IF (osname == null) osname =

""

IF (osname.tolowercase (). Indexof

"win")! = -1) {

Separator =

"//" if (storedir == null || stiledir.equals (")) Storedir =" c: // TMP ";} else {separator =" / "; storedir =" / tmp ";} file storefile = New file (Storedir Separator FileName); System.out.Println ("Storefile's Path:" StoreFile.toString ()); // for (int i = 0; storefile.exists (); i ) {// storefile = new File (storedir separator fileName i); //} BufferedOutputStream bos = null; BufferedInputStream bis = null; try {bos = new BufferedOutputStream (new FileOutputStream (storefile)); bis = new BufferedInputStream (in); int c WHILE ((c = bisread ())! = -1) {bos.write (c); bos.flush ();}} catch (exception exception) {exception.printStackTrace (); throw new exception (" File saving failed! ");} Finally {bos.close (); bis.close ();}} / ** * PRASEMIMEMESSAGE Test * / public static void main (string args []) THROWS Exception {string host =" Hostname / IP "; / / [Pop.mail.yahoo.com.cn] string username =" User Name "; // [WWP_1124] String Password =" Password "; // [........ 】 Properties PROPS = New Properties (); session se Ssion = session.getDefaultinstance (props, null); Store Store = session.getStore ("POP3"); Store.Connect (Host, Username, Password); Folder Folder = Store.getFolder ("Inbox"); Folder.Open Folder.read_only; message message [] = folder.getMess (); system.out.println ("Messages's length:" message.Length); PraseMimeMessage PMM = NULL; for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-110480.html

New Post(0)