import
javax.mail.Store;
import javax.mail. * ;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io. * ;
/**
*
* <p>Title: JavaMail</p>
* <p>Description:收取郵件 </p>
* <p>Copyright: Copyright (c) 2005</p>
* @author Derek.G
* @version 1.0
* 收取郵件:
1.獲取一個會話;
2.獲取并連接郵箱所在的存儲器(Store對象),打開該用戶的郵箱;
3.獲取所希望閱讀的消息;
4.關(guān)閉目錄和連接。
*/
public class ReadMail extends MailProcessBase{
// 初始化主機
String host = " pop3.usish.com " ;
String username = " derek_g " ;
String password = " 12140827 " ;
public ReadMail() throws Exception {
super ();
}
public void readMail(){
try {
// 獲取Store對象,使用POP3協(xié)議,也可能使用IMAP
System.out.println( " 獲取Store對象 " );
Store store = mailSession.getStore( " pop3 " );
// 連接到郵件服務(wù)器
System.out.println( " 連接到郵件服務(wù)器 " );
store.connect(host, username, password);
// 獲取該用戶Folder對象,并以只讀方式打開
System.out.println( " 獲取該用戶Folder對象 " );
Folder folder = store.getFolder( " INBOX " );
folder.open(Folder.READ_ONLY);
// 檢索所有郵件,按需填充
System.out.println( " 檢索所有郵件 " );
Message message[] = folder.getMessages();
// 遍歷每一個郵件
System.out.println( " 遍歷每一個郵件 " );
BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));
for ( int i = 0 , n = message.length; i < n; i ++ ) {
// 打印每個郵件的發(fā)件人和主題
System.out.println(i + " : " + message[i].getFrom()[ 0 ] + " \t " +
message[i].getSubject());
// 是否讀取該消息
System.out.println( " 是否讀取消息內(nèi)容? " + " [YES讀取 / QUIT結(jié)束] " );
String line = reader.readLine();
FileOutputStream fos = new FileOutputStream( " c:\\mail.txt " );
if (line.equals( " YES " )) {
message[i].writeTo(fos); // 這種方式只能顯示文本消息
System.out.println(message[i].getContent());
}
else break ;
}
System.out.println( " 收取郵件完畢! " );
folder.close( false );
store.close();
}
catch (NoSuchProviderException ex) {ex.printStackTrace();}
catch (MessagingException ex) {ex.printStackTrace();}
catch (IOException ex) { ex.printStackTrace(); }
}
public static void main(String[] args) {
try {
new ReadMail().readMail();
}
catch (Exception ex) {ex.printStackTrace(); }
}
}
import javax.mail. * ;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io. * ;
/**
*
* <p>Title: JavaMail</p>
* <p>Description:收取郵件 </p>
* <p>Copyright: Copyright (c) 2005</p>
* @author Derek.G
* @version 1.0
* 收取郵件:
1.獲取一個會話;
2.獲取并連接郵箱所在的存儲器(Store對象),打開該用戶的郵箱;
3.獲取所希望閱讀的消息;
4.關(guān)閉目錄和連接。
*/
public class ReadMail extends MailProcessBase{
// 初始化主機
String host = " pop3.usish.com " ;
String username = " derek_g " ;
String password = " 12140827 " ;
public ReadMail() throws Exception {
super ();
}
public void readMail(){
try {
// 獲取Store對象,使用POP3協(xié)議,也可能使用IMAP
System.out.println( " 獲取Store對象 " );
Store store = mailSession.getStore( " pop3 " );
// 連接到郵件服務(wù)器
System.out.println( " 連接到郵件服務(wù)器 " );
store.connect(host, username, password);
// 獲取該用戶Folder對象,并以只讀方式打開
System.out.println( " 獲取該用戶Folder對象 " );
Folder folder = store.getFolder( " INBOX " );
folder.open(Folder.READ_ONLY);
// 檢索所有郵件,按需填充
System.out.println( " 檢索所有郵件 " );
Message message[] = folder.getMessages();
// 遍歷每一個郵件
System.out.println( " 遍歷每一個郵件 " );
BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));
for ( int i = 0 , n = message.length; i < n; i ++ ) {
// 打印每個郵件的發(fā)件人和主題
System.out.println(i + " : " + message[i].getFrom()[ 0 ] + " \t " +
message[i].getSubject());
// 是否讀取該消息
System.out.println( " 是否讀取消息內(nèi)容? " + " [YES讀取 / QUIT結(jié)束] " );
String line = reader.readLine();
FileOutputStream fos = new FileOutputStream( " c:\\mail.txt " );
if (line.equals( " YES " )) {
message[i].writeTo(fos); // 這種方式只能顯示文本消息
System.out.println(message[i].getContent());
}
else break ;
}
System.out.println( " 收取郵件完畢! " );
folder.close( false );
store.close();
}
catch (NoSuchProviderException ex) {ex.printStackTrace();}
catch (MessagingException ex) {ex.printStackTrace();}
catch (IOException ex) { ex.printStackTrace(); }
}
public static void main(String[] args) {
try {
new ReadMail().readMail();
}
catch (Exception ex) {ex.printStackTrace(); }
}
}