Java 接收邮件的问题
package com.helian;
public class ReciveMail {
private MimeMessage msg = null;
private String saveAttchPath = "";
private StringBuffer bodytext = new StringBuffer();
private String dateformate = "yy-MM-dd HH:mm";
public ReciveMail(MimeMessage msg){
this.msg = msg;
}
public void setMsg(MimeMessage msg){
this.msg = msg;
}
/**
* 获取发送邮件者信息
* @return
* @throws
MessagingException */
public String getForm() throws MessagingException{
InternetAddress[] address = (InternetAddress[]) msg.getFrom();
String from = address[0].getAddress();
if(from == null){
from = "";
}
String personal = address[0].getPersonal();
if(personal == null){
personal = "";
}
String fromaddr = personal + "<" + from + ">";
return fromaddr;
}
/**
* 获取邮件收件人,抄送,密送的地址和信息。根据所传递的参数不同,“to” -->收件人 ,"cc" -->抄送人地址,"bcc" -->密送地址
* @param type
* @return
* @throws MessagingException
* @throws Unsupported
EncodingException */
/**
* 获取邮件主题
* @return
* @throws UnsupportedEncodingException
* @throws MessagingException
*/
public String getSubject() throws UnsupportedEncodingException,MessagingException{
String subject = "";
subject = MimeUtility.decodeText(msg.getSubject());
if(subject == null){
subject = "";
}
return subject;
}
/**
* 获取邮件发送日期
* @return
* @throws MessagingException
*/
public String getSendDate() throws MessagingException{
Date senDate = msg.getSentDate();
SimpleDateFormat smd = new SimpleDateFormat(dateformate);
return smd.format(senDate);
}
/**
* 获取邮件正文内容
* @return
*/
public String getBodyText(){
return bodytext.toString();
}
/**
* 解析邮件,将得到的邮件内容保存到一个stringBuffer对象中,解析邮件 主要根据MimeType的不同执行不同的操作,一步一步的解析
* @param part
* @throws MessagingException
* @throws
IOException */
public void getMailContent(Part part) throws MessagingException,IOException{
String contentType = part.getContentType();
int nameindex = contentType.indexOf("name");
boolean conname = false;
if(nameindex != -1){
conname = true;
}
System.out.println("CONTENTTYPE:" +contentType);
if(part.isMimeType("text/plain") &&! conname){
bodytext.append((String)part.getContent());
}else if(part.isMimeType("text/html") &&! conname){
bodytext.append((String)part.getContent());
}else if(part.isMimeType("multipart/*")){
Multipart multipart = (Multipart) part.getContent();
int count = multipart.getCount();
for (int i = 0; i < count; i++) {
getMailContent(multipart.getBodyPart(i));
}
}else if(part.isMimeType("message/rfc822")){
getMailContent((Part) part.getContent());
}
}
/**
* 判断邮件是否需要回执,如需要回执返回true,否则返回false;
* @return
* @throws MessagingException
*/
public boolean getReplySign() throws MessagingException{
boolean replySign = false;
String needreply[] = msg.getHeader("Disposition-Notification-TO");
if(needreply != null){
replySign = true;
}