javamail接收邮件的bean,可以正确的解析中文(转)(一)
这几天写的javamail项目中接收邮件和解析邮件的类,参考了很多代码,但是好像没有比较完整的对中文支持还有邮件解析的整合,所以自己写了一个。
Pop3Bean.java是主要的代码
FileProperties.java读取配置文件的类
MailConstants.java指向配置文件的接口,可以不用
Pop3BeanTest.java测试程序
ReadEml.java读取eml文件的bean
接收邮件类
//:Pop3Bean.java /*
- Created on 2004-12-22 *
- TODO To change the template for this generated file go to
- Window - Preferences - Java - Code Style - Code Templates */ package org.jteam.jmail.pop3bean;
import java.io.; import java.text.; import java.util.; import org.jteam.jmail.util.; import javax.mail.; import javax.mail.internet.;
/**
-
@author David
-
TODO To change the template for this generated type comment go to Window -
-
Preferences - Java - Code Style - Code Templates */ public class Pop3Bean {
private int mailCounter; //邮件计数
private int mailIndex; //邮件编号,即邮件在messages数组中的位置
private int mailDownErrorCounter; //正在下载邮件时,出错的计数器
private boolean[] recordFailure; //记录下载出错的邮件的序号
private int totalRetryTimes; //总共重试次数
private int retryTimeCounter; //记下重试的次数
private boolean otherError; //若是在邮件正式下载之前出错,则置该值为true
private String extension; //文件扩展名
private Store store;
private Folder folder;
private Message[] messages;
private Message message;
private Part part;
private String emlName;
private String attachName;
private int allMessageCount;
private int messageCount;
private String dateformat; //默认的日前显示格式
private String propFile = MailConstants.PROPS_FILE_NAME;//用这个接口类的好处是更改配置文件路径的时候不需要更改每个类
private String protocol; //服务协议
private String mailHost; //服务器地址
private String userName; //用户名
private String password; //密码
private String saveAttachPath; //附件下载后的存放目录
private String saveEmlPath;//保存eml文件的路径
public Pop3Bean() throws IOException { FileProperties fp = new FileProperties(propFile); fp.load(); protocol = fp.getProperty(MailConstants.RECV_PROTO); mailHost = fp.getProperty(MailConstants.RECV_HOST); userName = fp.getProperty(MailConstants.RECV_USER); password = fp.getProperty(MailConstants.RECV_PASS); saveAttachPath = fp.getProperty(MailConstants.RECV_ATTACH); saveEmlPath = fp.getProperty(MailConstants.RECV_ROOT); dateformat = fp.getProperty("mail.receive.dtfmat"); extension = fp.getProperty("mail.receive.extension"); totalRetryTimes = Integer .parseInt(fp.getProperty("mail.receive.retry")); }
/** * 设置邮件主机 */ public void setMailHost(String mailHost) { this.mailHost = mailHost; }
/** * 获取邮件主机 */ public String getMailHost() { return this.mailHost; }
/** * 设置邮件帐号 */ public void setUserName(String userName) { this.userName = userName; }
/** * 获取邮件帐号 */ public String getUserName() { return this.userName; }
/** * 设置邮件密码 */ public void setPassword(String password) { this.password = password; }
/** * 设置Store */ public void setStore(Store store) { this.store = store; }
/** * 设置邮箱文件夹 */ public void setFolder(Folder folder) { this.folder = folder; }
/** * 设置messages数组 */ public void setMessages(Message[] messages) { this.messages = messages; }
/** * 设置message */ public void setMessage(Message message) { this.message = message; }
/** * 获取message */ public Message getMessage() { return this.message; }
/** * 获取folder中的message数量 * * @throws MessagingException */ public int getAllMessageCount() throws MessagingException { this.allMessageCount = folder.getMessageCount(); return allMessageCount; }
/** * 设置allMessageCount * * @throws MessagingException */ private void setAllMessageCount() throws MessagingException { this.allMessageCount = this.folder.getMessageCount(); }
/** * 获取messages中message的数量 * * @return */ public int getMessageCount() { this.messageCount = this.messages.length; return messageCount; }
/** * 获得folder中新邮件的数量 * * @return * @throws MessagingException */ public int getNewMessageCount() throws MessagingException { return this.folder.getNewMessageCount(); }
/** * 获得folder中未读邮件的数量 * * @return * @throws MessagingException */ public int getUnreadMessageCount() throws MessagingException { return this.folder.getUnreadMessageCount(); }
/** * 获取Part */ public Part getPart() { return (Part) message; }
/** * 设置Part */ public void setPart(Part part) { this.part = part; }
/** * 设置附件存放路径 */
public void setAttachPath(String attachPath) { this.saveAttachPath = attachPath; }
/** * 获得附件存放路径 */
public String getAttachPath() { return saveAttachPath; }
/** * 设置eml存放路径 */
public void setEmlPath(String emlPath) { this.saveEmlPath = emlPath; }
/** * 获得eml存放路径 */
public String getEmlPath() { return saveEmlPath; }
public void setEmlName(String emlName) { this.emlName = emlName; }
public String getEmlName() { return emlName; }
public void setAttachName(String attachName) { this.attachName = attachName; }
public String getAttachName() { return attachName; }
public void setExtension(String extension) { this.extension = extension; }
public String getExtension() { return extension; }
/** * 设置日期显示格式 */
public void setDateFormat(String format) throws Exception { this.dateformat = format; }
请问楼主,你的就能全部解析? 目前还没找到完全解析的方法,很多邮件mime的格式不标准,纠结中...