commons-mail.jar 接收邮件问题
public void getMailMsg() throws Exception {final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";// Get a Properties objectProperties 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");// 以下步骤跟一般的JavaMail操作相同Session session = Session.getDefaultInstance(props, null);// 请将红色部分对应替换成你的邮箱帐号和密码URLName urln = new URLName("pop3", "pop.126.com", 995, null,"XXXX@126.com", "密码");Store store = session.getStore(urln);Folder inbox = null;try {store.connect();inbox = store.getFolder("INBOX");inbox.open(Folder.READ_ONLY);FetchProfile profile = new FetchProfile();profile.add(FetchProfile.Item.ENVELOPE);Message[] messages = inbox.getMessages();inbox.fetch(messages, profile);System.out.println("收件箱的邮件数:" + messages.length);Map<String,String> mailMap = new HashMap<String,String>();// 存放邮件的数据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() + ')');mailMap.put(MailMSG.FROM, ia.getAddress());// 邮件标题System.out.println("TITLE:" + messages[i].getSubject());mailMap.put(MailMSG.TITLE, messages[i].getSubject());// 邮件大小System.out.println("SIZE:" + messages[i].getSize());mailMap.put("SIZE", String.valueOf(messages[i].getSize()));// 邮件发送时间System.out.println("DATE:" + messages[i].getSentDate());mailMap.put("DATE", messages[i].getSentDate().toString());MailJobs.executeJob(mailMap );//根据发送的指令执行某个动做}} finally {try {inbox.close(false);} catch (Exception e) {}try {store.close();} catch (Exception e) {}}}protected static String decodeText(String text)throws UnsupportedEncodingException {if (text == null)return null;if (text.startsWith("=?GB") || text.startsWith("=?gb"))text = MimeUtility.decodeText(text);elsetext = new String(text.getBytes("GBK"));return text;}
?