View Mail Information List

Source: Internet
Author: User

The following is the Java code for viewing the mail Information List:

Import javax. Mail. store;
Import javax. Mail. fetchprofile;
Import java. util. properties;
Import javax. Mail. Session;
Import javax. Mail. folder;
Import javax. Mail. message;
Import javax. Mail. Internet. internetaddress;

Public class semail {
Public static void main (string [] ARGs ){
Try {

Properties props = new properties ();
Session S = session. getinstance (props );
Store store = S. getstore ("POP3 ");
Store. Connect ("pop.163.com", "bluebit_cn", "xiaohao ");

Folder folder = store. getfolder ("inbox ");
Folder. Open (Folder. read_write );

Fetchprofile profile = new fetchprofile ();
Profile. Add (fetchprofile. item. envelope );
Message arraymessage [] = folder. getmessages ();
Folder. Fetch (arraymessage, profile );

System. Out. println ("Number of inbox Emails:" + arraymessage. Length );
For (INT I = 0; I <arraymessage. length; I ++ ){
// Email sender
String from = arraymessage [I]. getfrom () [0]. tostring ();
Internetaddress IA = new internetaddress (from );
System. Out. println ("from:" + ia. getpersonal () + '(' +
IA. getaddress () + ')');
// Mail title
System. Out. println ("title:" + arraymessage [I]. getsubject ());
// Mail size
System. Out. println ("Size:" + arraymessage [I]. getsize ());
// Email sending time
System. Out. println ("Date:" + arraymessage [I]. getsentdate ());
}

Folder. Close (false );
Store. Close ();
} Catch (exception ee ){
Ee. printstacktrace ();
}
}
}

======================================
Question how do you delete a message from the mail server?
Derived from an unanswered question originally posed by Benjamin Alejandro Rodriguez Rengifo
Topics Java: API: javamail: IMAP4, Java: API: javamail: POP3
Author john zukoski
Created 23-feb-00 modified 25-jul-00

Answer

The basic process of deleting a message is to call setflag () on the message and set the flags. Flag. Deleted flag to true.

Message. setflag (flags. Flag. Deleted, true );

Then, when you close the folder, deleted messages will be removed.

Be sure to open the folder for read/write access:

Folder. Open (Folder. read_write );

The following program demonstrates listing each message in the folder and prompting for deletion:

Import java. Io .*;
Import javax. Mail .*;
Import javax. Mail. Internet .*;

Public class deletemessageexample {
Public static void main (string ARGs []) throws exception {
String host = ARGs [0];
String username = ARGs [1];
String Password = ARGs [2];

// Get session
Session session = session. getinstance (
System. getproperties (), null );

// Get the store
Store store = session. getstore ("POP3 ");
Store. Connect (host, username, password );

// Get folder
Folder folder = store. getfolder ("inbox ");
Folder. Open (Folder. read_write );

Bufferedreader reader = new bufferedreader (
New inputstreamreader (system. In ));

// Get directory
Message message [] = folder. getmessages ();
For (INT I = 0, n = message. length; I <n; I ++ ){
System. Out. println (I + ":" + message [I]. getfrom () [0]
+ "\ T" + message [I]. getsubject ());

System. Out. println ("Do you want to delete message? [Yes to delete] ");
String line = reader. Readline ();
// Mark as deleted if appropriate
If ("yes". Equals (line )){
Message [I]. setflag (flags. Flag. Deleted, true );
}
}

// Close connection
Folder. Close (true );
Store. Close ();
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.