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

📄 messagedialog1.java

📁 使用JavaMail开发的具有发送、接收邮件的应用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * MessageDialog.java * * Created on 2008年8月4日, 上午9:54 */package hjx.recvmail;import java.awt.Dimension;import java.awt.Toolkit;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.io.PrintStream;import java.util.logging.Level;import java.util.logging.Logger;import javax.mail.BodyPart;import javax.mail.Multipart;import javax.mail.Part;import javax.swing.DefaultListModel;import javax.swing.JFileChooser;import javax.swing.event.HyperlinkEvent;import javax.swing.event.HyperlinkListener;/** * * @author  hujx */public class MessageDialog1 extends javax.swing.JDialog {    private StringBuffer bodyText = new StringBuffer(); //存放邮件内容的StringBuffer对象     private DefaultListModel listModel = new DefaultListModel();    private String path = new File("mail/").getAbsolutePath() + "/";    /** Creates new form MessageDialog */    public MessageDialog1(java.awt.Frame parent, boolean modal) {        super(parent, modal);        initComponents();                // Center the dialog at the center of screen        int nWndWidth = getWidth();        int nWndHeight = getHeight();        Toolkit kit = Toolkit.getDefaultToolkit();        Dimension dim = kit.getScreenSize();        setBounds((dim.width - nWndWidth) / 2, (dim.height - nWndHeight) / 2,                nWndWidth, nWndHeight);    }    public void showMessage(InMessage message) {//        delAllFile("mail");        try {            txtSend.setText(message.send);            txtRecv.setText(message.to);            txtSentDate.setText(message.sentDate);            txtSubject.setText(message.subject);            if (message.html == null)                txtContent.setText(message.contentText);            else {                String str = "file:" + message.html;                txtContent.setEditable(false);                txtContent.setContentType("text/html");                txtContent.addHyperlinkListener(new HyperlinkListener() {                    public void hyperlinkUpdate(HyperlinkEvent e) {                        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {                            try {                                txtContent.setPage(e.getURL());                            } catch (IOException ex) {                                ex.printStackTrace();                            }                        }                    }                });                txtContent.setPage(str);            }            if (message.attaches != null)                for (String fileName :  message.attaches)                    listModel.addElement(fileName);                        this.setTitle(txtSubject.getText());            if (listModel.isEmpty())                jPanel2.setPreferredSize(new Dimension(0,0));        } catch (Exception ex) {            Logger.getLogger(MessageDialog1.class.getName()).log(Level.SEVERE, null, ex);        }    }    /**      * 解析邮件,把得到的邮件内容保存到一个StringBuffer对象中,解析邮件      * 主要是根据MimeType类型的不同执行不同的操作,一步一步的解析      */    public void getMailContent(Part part) throws Exception {        String contentType = part.getContentType();        //获得邮件的MimeType类型        System.out.println("邮件的MimeType类型: " + contentType);        int nameIndex = contentType.indexOf("name");        boolean conName = false;        if (nameIndex != -1) {            conName = true;        }        if (part.isMimeType("text/plain") && conName == false) {            // text/plain 类型            bodyText.append((String) part.getContent());            txtContent.setText(bodyText.toString());        } else if (part.isMimeType("text/html") && conName == false) {            // text/html 类型//            bodyText.append((String) part.getContent());            handleHtml(part);        } else if (part.isMimeType("multipart/*")) {            // multipart/*            Multipart multipart = (Multipart) part.getContent();            int counts = multipart.getCount();            for (int i = 0; i < counts; i++) {                getMailContent(multipart.getBodyPart(i));            }        } else if (part.isMimeType("message/rfc822")) {            // message/rfc822            getMailContent((Part) part.getContent());        } else if (conName) {            String disposition = part.getDisposition();            if ((disposition != null) && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) {                saveAttach((BodyPart)part);            }        }    }    //处理Html文件    public void handleHtml(Part msg) throws Exception {        String content = (String) msg.getContent();        String filename = "mail/" + (int) (Math.random() * 100 + 1) + ".html";        try {            FileOutputStream out = new FileOutputStream(filename);            PrintStream p = new PrintStream(out);            p.println(content);        } catch (FileNotFoundException e) {            e.printStackTrace();        }        File file = new File(filename);        String str = file.getAbsolutePath();        str = "file:" + str;        txtContent.setEditable(false);        txtContent.setContentType("text/html");        txtContent.addHyperlinkListener(new HyperlinkListener() {            public void hyperlinkUpdate(HyperlinkEvent e) {                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {                    try {                        txtContent.setPage(e.getURL());                    } catch (IOException ex) {                        ex.printStackTrace();                    }                }            }        });        txtContent.setPage(str);    }    private void saveAttach(BodyPart part) throws Exception {        //文件名一般都经过了base64编码,下面是解码//        String fileName = MimeUtility.decodeText(part.getFileName());        String fileName = JavaMailFrame.decodeHeader(part.getFileName());        System.out.println("有附件:" + fileName);        InputStream in = part.getInputStream();        FileOutputStream writer = new FileOutputStream(new File("mail/" + fileName));        byte[] content = new byte[255];        int read;        while ((read = in.read(content)) != -1) {            writer.write(content);        }        writer.close();        in.close();                listModel.addElement(fileName);    }    public static void delFolder(String folderPath) {        try {            delAllFile(folderPath); // 删除完里面所有内容            java.io.File myFilePath = new java.io.File(folderPath);            myFilePath.delete(); // 删除空文件夹        } catch (Exception e) {            e.printStackTrace();        }    }    /**     * 删除指定文件夹下所有文件     * @param path: 文件夹完整绝对路径     * @return     */    public static boolean delAllFile(String path) {        boolean flag = false;        File file = new File(path);        if (!file.exists()) {            return flag;        }        if (!file.isDirectory()) {            return flag;        }        String[] tempList = file.list();        File temp = null;        int i, nCount = tempList.length;        for (i = nCount - 1; i >= 0; i--) {            if (path.endsWith(File.separator)) {                temp = new File(path + tempList[i]);            } else {                temp = new File(path + File.separator + tempList[i]);            }            if (temp.isFile()) {

⌨️ 快捷键说明

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