利用JavaMail通过POP3协议、IMAP协议收取Gmail邮箱

POP3和IMAP的区别到处都有,这里不再敖述,只通过例子说明如下:

                  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		// Get a Properties object
		Properties props = System.getProperties();
		props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.pop3.socketFactory.fallback", "false");
		props.setProperty("mail.pop3.port", "995");
		props.setProperty("mail.pop3.socketFactory.port", "995");
		props.setProperty("mail.pop3.disabletop", "true");
		props.setProperty("mail.pop3.ssl.enable", "true");
		props.setProperty("mail.pop3.useStartTLS", "true");

		// 以下步骤跟一般的JavaMail操作相同
		Session session = Session.getDefaultInstance(props, null);
		URLName urln = new URLName("pop3", "pop.gmail.com", 995, null, username, password);
		Store store = session.getStore(urln);
		store.connect();


 

Folder inbox = null;
		try {
			store.connect();
			
			inbox = store.getFolder("INBOX");
			inbox.open(Folder.READ_ONLY);
			FetchProfile profile = new FetchProfile();
			profile.add(FetchProfile.Item.ENVELOPE);
//			profile.add(FetchProfile.Item.CONTENT_INFO);
//			profile.add(FetchProfile.Item.FLAGS);
			profile.add(UIDFolder.FetchProfileItem.UID);
			Message[] messages = inbox.getMessages();
			inbox.fetch(messages, profile);
			System.out.println("收件箱的邮件数:" + messages.length);
			for (int i = 0; i < messages.length; i++) {
				
				// 邮件发送者
				String from = decodeText(messages[i].getFrom()[0].toString());
				InternetAddress ia = new InternetAddress(from);
				System.out.println("FROM:" + ia.getPersonal() + '(' + ia.getAddress() + ')');
				// 邮件标题
				System.out.println("TITLE:" + messages[i].getSubject());
				// 邮件大小
				System.out.println("SIZE:" + messages[i].getSize());
				// 邮件发送时间
				System.out.println("DATE:" + messages[i].getSentDate());
				//邮件ID
				String uid = ((POP3Folder)inbox).getUID(messages[i]);
				System.out.println("UID:"+ uid);
				//收件人
				String to = decodeText(messages[i].getAllRecipients()[0].toString());
				InternetAddress iaTo = new InternetAddress(to);
				System.out.println("TO:" + iaTo.getPersonal() + '(' + iaTo.getAddress() + ')');
				//内容
//				String disposition; 
//				Multipart mp = (Multipart) messages[i].getContent(); 
//				int mpCount = mp.getCount(); 
//				System.out.println("");
//				for (int m = 0; m < mpCount; m++){ 
//
//					BodyPart part = mp.getBodyPart(m); 
//					disposition = part.getDisposition(); 
//					if (disposition != null && disposition.equals(Part.ATTACHMENT)){ 
//						//saveAttach(part,Constant.ZIP_DIR); 
//					} else{ 
//						//System.out.println(part.getContent()); 
//					} 
//				}
				File f = new File("/mail/test"+uid+".eml");
				messages[i].writeTo(new FileOutputStream(f)); 
			}
		} finally {
			try {
				inbox.close(false);
			} catch (Exception e) {
			}
			try {
				store.close();
			} catch (Exception e) {
			}

		}


IMAP的见另一篇

你可能感兴趣的:(JAVA)