messageview.java

来自「基于JavaMail开发的E-mail系统」· Java 代码 · 共 52 行

JAVA
52
字号
// MessageView.java

import javax.mail.*;

public class MessageView {

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

		String host = args[0];
		String user = args[1];
		String password = args[2];
		int msgNum = Integer.parseInt(args[3]);

		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:  Display the subject and date for each message
			if (msgNum <= inbox.getMessageCount()) {
				Message theMessage = inbox.getMessage(msgNum);
				theMessage.writeTo(System.out);
			}
			else {
				System.out.println("Message number not found");
			}

			// Step 5:  Close up shop
			inbox.close(false);
			msgStore.close();
		}
		catch (Exception exc) {
			exc.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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