📄 mailmsg.java
字号:
package cn.js.fan.mail;
import java.util.Date;
import java.util.Vector;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import cn.js.fan.util.*;
import org.apache.log4j.Logger;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class MailMsg {
String[] Recipients = null;
String Subject = "";
String Sender = "";
String Content = "";
Date ReceiveDate = null;
Date SentDate = null;
Vector attachs = null;
Message message = null;
String From = "";
int Size = 0;
String ID = "";
String content = ""; //邮件正文内容
int dispnum = 0; //正文、附件在邮件中的序号
boolean HasAttachment = false;
Logger logger = Logger.getLogger( MailMsg.class.getName() );
public MailMsg() {
attachs = new Vector();
}
public MailMsg(Message message) {
this.message = message;
}
/**
* 对邮件进行初始化
* @param message
* @param isdetail 是否取出详细信息
*/
public MailMsg(Message message, boolean isdetail) {
this.message = message;
attachs = new Vector();
init(isdetail);
}
public Message getMessage() {
return message;
}
/**
* 取得序号为num的附件
* @param num
*/
public Attachment getAttachment(int num) {
Attachment a = null;
try {
Part messagePart = message;
Object content = messagePart.getContent();
if (! (content instanceof Multipart))
return null;
Multipart multipart = (Multipart)content;
Part part = multipart.getBodyPart(num);
String disposition = part.getDisposition();
if (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE)) {
a = new Attachment(part, num);
}
}
catch (Exception e) {
logger.error("getAttachment: "+e.getMessage());
}
return a;
}
public void init(boolean isdetail) {
if (message==null)
return;
try {
From = ((InternetAddress)message.getFrom()[0]).getPersonal();//取出第一个发送者
if(From==null)
From = ((InternetAddress)message.getFrom()[0]).getAddress();
Sender = ((InternetAddress)message.getFrom()[0]).getAddress();
Subject = message.getSubject();
Address[] addrs = message.getAllRecipients();
if (addrs!=null) {
Recipients = new String[addrs.length];
for (int i = 0; i < addrs.length; i++) {
Recipients[i] = ( (InternetAddress) addrs[i]).getAddress();
}
}
ReceiveDate = message.getReceivedDate();//接收时间
SentDate = message.getSentDate();
Size = message.getSize();
//ID = ((MimeMessage)message).getMessageID();
ID = ""+message.getMessageNumber();
//System.out.print("ID="+ID);
//取得附件
Part messagePart = message;
Object content = messagePart.getContent();
attachs.removeAllElements();
dispnum = 0; //初始化序号
if (content instanceof Multipart) {
//处理正文及附件部分
handleMultipart((Multipart)content,isdetail);
} else {
//处理正文部分
handlePart(messagePart,isdetail);
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public boolean hasAttachment() {
return HasAttachment;
}
public void handleMultipart(Multipart multipart, boolean isdetail) throws
MessagingException, IOException {
for (int i = 0, n = multipart.getCount(); i < n; i++) {
handlePart(multipart.getBodyPart(i), isdetail);
dispnum ++;
}
}
public void handlePart(Part part, boolean isdetail) throws MessagingException,
IOException {
String disposition = part.getDisposition();
String contentType = part.getContentType();
if (disposition == null) { // When just body,只有body时
//Debug.println("Null: " + contentType);
if (isdetail) {
if (contentType.startsWith("text/plain") ||
contentType.startsWith("text/html")) {
InputStream is = part.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String thisLine = reader.readLine();
while (thisLine != null) {
Content += thisLine;
thisLine = reader.readLine();
}
is.close();
}
}
}
else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
//attachs.addElement(StrUtil.UnicodeToGB(getISOFileName(part)));//这行与下行都对
HasAttachment = true;
if (isdetail) {
Attachment a = new Attachment(part, dispnum); //取得附件信息
attachs.addElement(a);
}
//saveFile(part.getFileName(), part.getInputStream());
}
else if (disposition.equalsIgnoreCase(Part.INLINE)) {
HasAttachment = true;
if (isdetail) {
//attachs.addElement(StrUtil.UnicodeToGB(getISOFileName(part)));
Attachment a = new Attachment(part, dispnum);
attachs.addElement(a);
}
//saveFile(part.getFileName(), part.getInputStream());
}
else { // Should never happen
logger.error("Other: " + disposition);
}
}
/**
* @从BodyPart中提取使用ISO-8859-1编吗的文件名,与part.getFileName()得到的结果是一样的
* @因为BodyPart.getFilename()过程已经对文件名作了一次编码,有时不能直接使用
*/
public static String getISOFileName(Part body) {
//设置一个标志,判断文件名从Content-Disposition中获取还是从Content-Type中获取
boolean flag = true;
if (body == null) {
return null;
}
String[] cdis;
try {
cdis = body.getHeader("Content-Disposition");
}
catch (Exception e) {
return null;
}
if (cdis == null) {
flag = false;
}
if (!flag) {
try {
cdis = body.getHeader("Content-Type");
}
catch (Exception e) {
return null;
}
}
if (cdis == null) {
return null;
}
if (cdis[0] == null) {
return null;
}
//从Content-Disposition中获取文件名
if (flag) {
int pos = cdis[0].indexOf("filename=");
if (pos < 0) {
return null;
}
//如果文件名带引号
if (cdis[0].charAt(cdis[0].length() - 1) == '"') {
return cdis[0].substring(pos + 10, cdis[0].length() - 1);
}
return cdis[0].substring(pos + 9, cdis[0].length());
}
else {
int pos = cdis[0].indexOf("name=");
if (pos < 0) {
return null;
}
//如果文件名带引号
if (cdis[0].charAt(cdis[0].length() - 1) == '"') {
return cdis[0].substring(pos + 6, cdis[0].length() - 1);
}
return cdis[0].substring(pos + 5, cdis[0].length());
}
}
public Vector getAttachments() {
return attachs;
}
public static void saveFile(String filename,
InputStream input) throws IOException {
if (filename == null) {
filename = File.createTempFile("xx", ".out").getName();
}
// Do no overwrite existing file
File file = new File("e:/mail.doc");//+filename);
for (int i = 0; file.exists(); i++) {
file = new File(filename + i);
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);
int aByte;
while ( (aByte = bis.read()) != -1) {
bos.write(aByte);
}
bos.flush();
bos.close();
bis.close();
}
public String getID() {
return ID;
}
public int getSize() {
return this.Size;
}
public void setSize(int s) {
this.Size = s;
}
public Date getSentDate() {
return this.SentDate;
}
public void setSentDate(Date d) {
this.SentDate = d;
}
public String getSubject() {
return this.Subject;
}
public void setSubject(String s) {
this.Subject = s;
}
public String getContent() {
return this.Content;
}
public void setContent(String s) {
this.Content = s;
}
public Date getReceiveDate() {
return this.ReceiveDate;
}
public void setReceiveDate(Date d) {
this.ReceiveDate = d;
}
public void setSender(String s) {
this.Sender = s;
}
public String getSender() {
return this.Sender;
}
public String getFrom() {
return this.From;
}
public String[] getRecipients() {
return this.Recipients;
}
public void setFrom(String f) {
this.From = f;
}
public boolean isDraft() {
try {
if (message.isSet(Flags.Flag.DRAFT))
return true;
else
return false;
}
catch (Exception e) {
logger.error("isDraft:"+e.getMessage());
}
return false;
}
public boolean isDeleted() {
try {
if (message.isSet(Flags.Flag.DELETED))
return true;
else
return false;
}
catch (Exception e) {
logger.error("isDeleted:"+e.getMessage());
}
return false;
}
public boolean isSeen() {
try {
// Check if DELETED flag is set of this message
if (message.isSet(Flags.Flag.SEEN))
return true;
else
return false;
// Examine ALL system flags for this message
/*
Flags flags = message.getFlags();
Flags.Flag[] sf = flags.getSystemFlags();
for (int i = 0; i < sf.length; i++) {
if (sf[i] == Flags.Flag.DELETED)
System.out.println("DELETED message");
else if (sf[i] == Flags.Flag.SEEN)
System.out.println("SEEN message");
else if (sf[i] == Flags.Flag.DRAFT)
System.out.println("DRAFT message");
}
*/
}
catch (Exception e) {
logger.error("isSeen:"+e.getMessage());
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -