Java mail 接收邮件

public class JavaMailReadMailTest {

	public static void main(String args[]) throws MessagingException,
			IOException {
		String pop3Host = "pop3.sohu.com";
		String user = "******@sohu.com";
		String pass = "******";
		Properties properties = System.getProperties();
		Session session = Session.getInstance(properties);
		Store store = session.getStore("pop3");
		store.connect(pop3Host, user, pass);

		Folder folder = store.getFolder("INBOX");
		folder.open(Folder.READ_ONLY);
		Message[] messages = folder.getMessages();

		for (int i = 0; i < messages.length; i++) {
			System.out.println("\nFrom:" + messages[i].getFrom()[0]+ "\n"
					+ "Subject:" + messages[i].getSubject());
			ContentType ct = new ContentType(messages[i].getContentType());
			if ("text/html".equalsIgnoreCase(ct.getBaseType())) {
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(messages[i].getInputStream()));
				String s;
				while ((s = reader.readLine()) != null) {
					System.out.println(s);
				}
			} else {
				Object o = messages[i].getContent();
				if (o instanceof String) {
					System.out.println(o);
				} else {
					System.out.println(messages[i].getContentType());
					if (o instanceof MimeMultipart) {
						MimeMultipart mp = (MimeMultipart) o;
						for (int j = 0; j < mp.getCount(); ++j) {
							MimeBodyPart bp = (MimeBodyPart) mp.getBodyPart(j);
							System.out.println(bp.getContentType());
						}
					}
				}
			}

		}
	}
}

 

你可能感兴趣的:(java,html,J#)