package success;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
/**
* 解析eml格式邮件(已测试)
* @author Lancelot
*
*/
public class Mail {
private MimeMessage mimeMessage = null;
private StringBuffer content = new StringBuffer();
private StringBuffer str = new StringBuffer();
private String saveAttachPath = ""; // 附件下载后的存放目录
private String dateformat = "yyyy-MM-dd HH:mm"; // 设置日期显示格式
public Mail() {
}
public Mail(MimeMessage mimeMessage) {
this.mimeMessage = mimeMessage;
}
public void setMimeMessage(MimeMessage mimeMessage) {
this.mimeMessage = mimeMessage;
}
/** 获得邮件标题 *
* @throws MessagingException */
public String getSubject() throws MessagingException {
String subject = null;
subject = mimeMessage.getSubject();
if (subject == null)
subject = "";
return subject;
}
/** 获得发件人的姓名和地址 *
* @throws MessagingException */
public String getFrom() throws MessagingException {
InternetAddress address[] = null;
address = (InternetAddress[]) mimeMessage.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;
}
/** 获得邮件发送时间 *
* @throws MessagingException */
public String getSentDate() throws MessagingException {
Date sentdate = mimeMessage.getSentDate();
SimpleDateFormat format = new SimpleDateFormat(dateformat);
return format.format(sentdate);
}
/** 判断是否需要回执的邮件 true or false *
* @throws MessagingException */
public boolean getReplySign() throws MessagingException {
boolean replysign = false;
String needreply[] = mimeMessage
.getHeader("Disposition-Notification-To");
if (needreply != null) {
replysign = true;
}
return replysign;
}
/** 获得邮件的Message-ID *
* @throws MessagingException */
public String getMessageId() throws MessagingException {
return mimeMessage.getMessageID();
}
/** 判断此邮件是否已读 true or false *
* @throws MessagingException */
public boolean isNew() throws MessagingException {
boolean isnew = false;
Flags flags = ((Message) mimeMessage).getFlags();
Flags.Flag[] flag = flags.getSystemFlags();
for (int i = 0; i < flag.length; i++) {
if (flag[i] == Flags.Flag.SEEN) {
isnew = true;
break;
}
}
return isnew;
}
/** 获取邮件内容 *
* @throws MessagingException
* @throws IOException */
public String getMailContent(Part msg) throws MessagingException,
IOException {
//临时存储图片的数量
int temp = 0;
//临时存储图片文件名
String tempname = "lancelot~-";
// 获取邮件的内容,包含所有邮件内容(正文+附件)
Multipart multipart = (Multipart) msg.getContent();
// System.out.println("邮件共有" + multipart.getCount() + "部分组成");
// 依次处理各个部分
for (int j = 0, n = multipart.getCount(); j < n; j++) {
// System.out.println("处理第" + j + "部分");
Part part = multipart.getBodyPart(j);//解包
// 判断此包裹内容是不是一个小包裹
if (part.getContent() instanceof Multipart) {
// 转成小包裹
Multipart p = (Multipart) part.getContent();
// System.out.println("小包裹中有" + p.getCount() + "部分");
// 列出小包裹中的内容
for (int k = 0; k < p.getCount(); k++) {
// 获取邮件中的图片
if (p.getBodyPart(k).getContentType().startsWith(
"image/jpeg")) {
FileOutputStream fos = new FileOutputStream("d:\\"
+ p.getBodyPart(k).getFileName());
InputStream in = p.getBodyPart(k).getInputStream();
byte[] by = new byte[8192];
int x = 0;
System.out.println(x);
while ((x = in.read(by)) != -1) {
fos.write(by);
}
fos.flush();
fos.close();
temp = temp + 1;
tempname = tempname + p.getBodyPart(k).getFileName()
+ "~-";
}
}
}
}
// 把邮件内容加入到StringBuffer中
content.append(getContent(msg));
// 替换掉图片的SRC
if (temp != 0) {
String t[] = tempname.split("~-");
int start = content.toString().indexOf("<IMG");
int end = content.toString().indexOf(">", start) + 1;
for (int x = 0; x < temp; x++) {
content
.replace(start, end, "<IMG SRC='D:\\" + t[x + 1]
+ "' >");
start = content.toString().indexOf("<IMG", end);
end = content.toString().indexOf(">", start) + 1;
}
}
return content.toString();
}
/** 获取邮件内容 *
* @throws MessagingException
* @throws IOException */
public String getContent(Part part) throws MessagingException, IOException {
String contentType = "";
contentType = part.getContentType();
int nameindex = contentType.indexOf("name");
boolean conname = false;
if (nameindex != -1)
conname = true;
// if(part.isMimeType("text/plain") && !conname){
// str.append(part.getContent());
// }else
if (part.isMimeType("text/html") && !conname) {
str.append(part.getContent());
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int counts = multipart.getCount();
for (int i = 0; i < counts; i++) {
getContent(multipart.getBodyPart(i));
}
} else if (part.isMimeType("message/rfc822")) {
getContent((Part) part.getContent());
}
return str.toString();
}
/** 判断邮件是否包含附件 *
* @throws IOException
* @throws MessagingException */
public boolean isContainAttach(Part part) throws MessagingException,
IOException {
boolean attachflag = false;
if (part.isMimeType("multipart/*")) {
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart mpart = mp.getBodyPart(i);
String disposition = mpart.getDisposition();
if ((disposition != null)
&& ((disposition.equals(Part.ATTACHMENT)) || (disposition
.equals(Part.INLINE))))
attachflag = true;
else if (mpart.isMimeType("multipart/*")) {
attachflag = isContainAttach((Part) mpart);
} else {
String contype = mpart.getContentType();
if (contype.toLowerCase().indexOf("application") != -1)
attachflag = true;
if (contype.toLowerCase().indexOf("name") != -1)
attachflag = true;
}
}
} else if (part.isMimeType("message/rfc822")) {
attachflag = isContainAttach((Part) part.getContent());
}
return attachflag;
}
/** 保存附件 *
* @throws Exception
* @throws MessagingException
* @throws IOException */
public void saveAttachMent(Part part) throws IOException,
MessagingException, Exception {
String fileName = "";
if (part.isMimeType("multipart/*")) {
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart mpart = mp.getBodyPart(i);
String disposition = mpart.getDisposition();
if ((disposition != null)
&& ((disposition.equals(Part.ATTACHMENT)) || (disposition
.equals(Part.INLINE)))) {
fileName = mpart.getFileName();
if (fileName.toLowerCase().indexOf("gb2312") != -1) {
fileName = MimeUtility.decodeText(fileName);
}
saveFile(fileName, mpart.getInputStream()