⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagelist.java

📁 基于JavaMail开发的E-mail系统
💻 JAVA
字号:
// MessageList.java

import javax.mail.*;

/**
 *  Demo class for listing messages on a POP3 server using the JavaMail API.
 *
 *  @author Chad (shod) Darby, darby@j-nine.com
 */
public class MessageList {

	public static void main(String[] args) {
		if (args.length != 3) {
			System.out.println("Usage:  java MessageList <pop3host> <user> <password>");
			System.exit(1);
		}

		String host = args[0];
		String user = args[1];
		String password = args[2];

		try {
			// Step 1:  Configure the mail session
			System.out.println("Configuring mail session for: " + host);
			java.util.Properties props = new java.util.Properties();
			props.put("mail.pop3.host", host);
			Session mailSession = Session.getDefaultInstance(props);

			// Step 2:  Retrieve and connect to the Store
			System.out.println("Connecting to message store: " + host);
			Store msgStore = mailSession.getStore("pop3");
			msgStore.connect(host, user, password);
			System.out.println("Connected!");

			// Step 3:  Retrieve the INBOX  folder
			Folder inbox = msgStore.getDefaultFolder().getFolder("INBOX");
			inbox.open(Folder.READ_ONLY);

			// Step 4:  Retrieve a list of messages
			Message[] msgs = inbox.getMessages();
			FetchProfile profile = new FetchProfile();
			profile.add(FetchProfile.Item.ENVELOPE);
			inbox.fetch(msgs, profile);

			// Step 5:  Display the subject and date for each message
			int count = inbox.getMessageCount();
			for (int i=0; i < count; i++) {
				System.out.println("Message #" + msgs[i].getMessageNumber());
				System.out.println("Subject: " + msgs[i].getSubject());
				System.out.println("Date: " + msgs[i].getSentDate());
				System.out.println("------------------------------------\n");
			}

			// Step 6:  Close up shop
			inbox.close(false);
			msgStore.close();
		}
		catch (javax.mail.MessagingException exc) {
			exc.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -