![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
|||||
| 改寫後的使用javamail pop3收信的javabean | |||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-9-2 ![]() |
|||||
|
正在装载数据…… /**Author:tyfun *DateTime:2003.01.10 *Package:com.westarsoft.mail */ package com.westarsoft.mail; import javax.mail.*; import javax.mail.internet.*; import java.util.*; import java.io.*; public class GetMail { public static String receive(String popServer, String popUser, String popPassword) { String mailContent = ""; Store store = null; Folder folder = null; try { Properties props = System.getProperties(); Session session = Session.getDefaultInstance(props, null); store = session.getStore("pop3"); store.connect(popServer, popUser, popPassword); folder = store.getDefaultFolder(); if(folder == null) throw new Exception("No default folder"); folder = folder.getFolder("INBOX"); if(folder == null) throw new Exception("No POP3 INBOX"); folder.open(Folder.READ_ONLY); Message[] msgs = folder.getMessages(); for(int msgNum = 0; msgNum < msgs.length; msgNum++) { mailContent = mailContent + getMessage(msgs[msgNum]) + "\n\n\n\n"; } } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (folder!=null) folder.close(false); if (store!=null) store.close(); } catch (Exception ex2) { ex2.printStackTrace(); } } return mailContent; } public static String getMessage(Message message) { String mailContent = null; try { String from = ((InternetAddress)message.getFrom()[0]).getPersonal(); if(from==null) from = ((InternetAddress)message.getFrom()[0]).getAddress(); mailContent = "FROM: "+from; String subject = message.getSubject(); mailContent = mailContent + "\n" +"SUBJECT: "+subject; Part messagePart = message; Object content = messagePart.getContent(); if(content instanceof Multipart) { messagePart = ((Multipart)content).getBodyPart(0); mailContent = mailContent + "\n" +"[ Multipart Message ]"; } mailContent = mailContent + "\n" +"CONTENT: "+content.toString(); String contentType = messagePart.getContentType(); mailContent = mailContent + "\n" +"CONTENT:"+contentType; if(contentType.startsWith("text/plain") || contentType.startsWith("text/html")) { InputStream is = messagePart.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String thisLine = reader.readLine(); while(thisLine!=null) { mailContent = mailContent + "\n" +thisLine; thisLine = reader.readLine(); } } } catch(Exception ex) { ex.printStackTrace(); } return mailContent; } } <% GetMail mail = new GetMail(); String content = mail.receive("pop3.server.com","user","password"); if((content.trim() == null)||(content.trim() == "")) { System.out.println("No mail!"); } else { System.out.println("You got a new mail!"); } %> 本文来源:http://blog.csdn.net/jdgdg/archive/2007/07/08/1682613.aspx
|
|||||
| 文章录入:admin 责任编辑:admin | |||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | |||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|