⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mailmsg.java

📁 cwbbs 云网论坛源码
💻 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;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 = "";    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;    }        public MailMsg(Message message, boolean isdetail) {        this.message = message;        attachs = new Vector();        init(isdetail);    }    public Message getMessage() {        return message;    }        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 static String decodeSubject(String subjectStr) {                if (!subjectStr.startsWith("=?") || !subjectStr.endsWith("?=") ||            subjectStr.indexOf("?B?") <= 0)            return subjectStr;        String subj = subjectStr;                String code = "gb2312";        subjectStr = subjectStr.substring(code.length() + 5, subjectStr.length() - 2);                sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();        try {            subjectStr = new String(decoder.decodeBuffer(subjectStr), code);        } catch (Exception e) {            e.printStackTrace();            return subj;        }        return subjectStr;    }    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();                        boolean isNeedDecode = false;            if (Subject.indexOf("=?x-unknown?") >= 0) {                Subject = Subject.replaceAll("x-unknown", "gb2312");                 isNeedDecode = true;            }            else if (Subject.indexOf("=?UNKNOWN") >= 0) {                Subject = Subject.replaceAll("UNKNOWN", "gb2312");                isNeedDecode = true;            }            else if (Subject.indexOf("=?GB2312") >=0) {                Subject = MimeUtility.decodeText(Subject);                isNeedDecode = true;            }            else if (Subject.indexOf("=?gb2312") >=0) {                Subject = MimeUtility.decodeText(Subject);                isNeedDecode = true;            }            String charset = getCharset(message);            if (isNeedDecode)                Subject = decodeSubject(Subject);            else {                                if (charset.equalsIgnoreCase("gb2312") || charset.equals("") || message.getContentType().indexOf("multipart/mixed")!=-1 || charset.equalsIgnoreCase("GBK")) {                    String Subject2 = new String(Subject.getBytes("ISO-8859-1"),                                         "gb2312");                                        if (StrUtil.UTF8Len(Subject) > StrUtil.UTF8Len(Subject2))                        ;                    else                        Subject = Subject2;                }            }                                    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 = "" + message.getMessageNumber();                                    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 String getCharset(Part part) throws MessagingException {        String charset = "";        String contentType = part.getContentType();        if (contentType.startsWith("text/plain") ||            contentType.startsWith("text/html")) {            int p = contentType.indexOf("charset");            if (p != -1) {                p = contentType.indexOf("=", p + 7);                if (p != -1)                    charset = contentType.substring(p + 1).trim();            }            if (charset.startsWith("\""))                charset = charset.substring(1);            if (charset.endsWith("\""))                charset = charset.substring(0, charset.length() - 1);        }        return charset;    }    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) {                         if (isdetail) {                if (contentType.startsWith("text/plain") ||                    contentType.startsWith("text/html")) {                    String charset = "utf-8";                    int p = contentType.indexOf("charset");                    if (p != -1) {                        p = contentType.indexOf("=", p + 7);                        if (p != -1)                            charset = contentType.substring(p + 1).trim();                    }                    if (charset.startsWith("\""))                        charset = charset.substring(1);                    if (charset.endsWith("\""))                        charset = charset.substring(0, charset.length() - 1);                                        InputStream is = part.getInputStream();                    BufferedReader reader = new BufferedReader(new                            InputStreamReader(is, charset));                    String thisLine = reader.readLine();                    while (thisLine != null) {                        content += thisLine;                        thisLine = reader.readLine();                    }                    is.close();                                        if (contentType.startsWith("text/plain"))                        content = StrUtil.toHtml(content);                }            }        } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {                        HasAttachment = true;            if (isdetail) {                Attachment a = new Attachment(part, dispnum);                 attachs.addElement(a);            }                    } else if (disposition.equalsIgnoreCase(Part.INLINE)) {            HasAttachment = true;            if (isdetail) {                                Attachment a = new Attachment(part, dispnum);                attachs.addElement(a);            }                    } else {             logger.error("Other: " + disposition);        }    }        public static String getISOFileName(Part body) {                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;        }                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();        }                File file = new File("e:/mail.doc");         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 {                        if (message.isSet(Flags.Flag.SEEN))                return true;            else                return false;                                } catch (Exception e) {            logger.error("isSeen:" + e.getMessage());        }        return false;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -