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

📄 javamailframe.java

📁 使用JavaMail开发的具有发送、接收邮件的应用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        msgDlg.setVisible(true);    }}//GEN-LAST:event_jTable1MouseClicked    private void addToTable(final MimeMessage message) {        try {            InMessage inMsg = new InMessage();            getMailContent(message, inMsg);            inMsg.UID = folder.getUID(message);            inMsg.send = getFrom(message);            inMsg.subject = getSubject(message);            inMsg.sentDate = getSentDate(message);            inMsg.size = getSize(message);            inMsg.to = getTo(message);            inMsgs.add(inMsg);            UIDs.add(inMsg.UID);                        ImageIcon ico = null;            if (inMsg.attaches != null && inMsg.attaches.size() > 0)                ico = attachIco;            ((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{inMsg.send, inMsg.subject, inMsg.sentDate, inMsg.size, inMsg, ico});        } catch (Exception ex) {            Logger.getLogger(JavaMailFrame.class.getName()).log(Level.SEVERE, null, ex);        }    }    /**      * 解析邮件,把得到的邮件内容保存到一个StringBuffer对象中,解析邮件      * 主要是根据MimeType类型的不同执行不同的操作,一步一步的解析      */    public void getMailContent(Part part, InMessage inMsg) 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 类型            inMsg.contentText = (String) part.getContent();        } else if (part.isMimeType("text/html") && conName == false) {            // text/html 类型            handleHtml(part, inMsg);        } 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), inMsg);            }        } else if (part.isMimeType("message/rfc822")) {            // message/rfc822            getMailContent((Part) part.getContent(), inMsg);        } else if (conName) {            String disposition = part.getDisposition();            if ((disposition != null) && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) {                saveAttach(part, inMsg);            }        }    }    //处理Html文件    public void handleHtml(Part msg, InMessage inMsg) throws Exception {        String content = (String) msg.getContent();        String filename = "mail/" + (int) (Math.random() * 1000000000 + 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();        inMsg.html = str;    }    private void saveAttach(Part part, InMessage inMsg) throws Exception {        //文件名一般都经过了base64编码,下面是解码        String fileName = 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();                if (inMsg.attaches == null)            inMsg.attaches = new ArrayList<String>();        inMsg.attaches.add(fileName);    }    /**     * 获得发件人的地址和姓名     */    public static String getFrom(Message msg) {        String from = "";        try {            if (msg.getFrom()[0] != null) {                from = msg.getFrom()[0].toString();            }            from = decodeHeader(from);        } catch (Exception e) {            e.printStackTrace();        }        return from;    }    /**     *  获得收件人的地址和姓名     */    public static String getTo(Message msg) {        String to = "";        try {            if (msg.getRecipients(Message.RecipientType.TO) != null) {                 for (Address rec : msg.getRecipients(Message.RecipientType.TO))                    to += decodeHeader(rec.toString()) + ";";            }        } catch (Exception ex) {            Logger.getLogger(JavaMailFrame.class.getName()).log(Level.SEVERE, null, ex);        }        return to;    }        private static String toChinese(String strvalue) {        try {            if (strvalue == null) {                return null;            } else {                strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");                return strvalue;            }        } catch (Exception e) {            e.printStackTrace();            return null;        }    }    public static String decodeHeader(String value) throws UnsupportedEncodingException {        if (value.startsWith("=?GB") || value.startsWith("=?gb") || value.startsWith("=?utf-8") || value.startsWith("=?UTF-8")) {            value = MimeUtility.decodeText(value);        }        else {            value = toChinese(value);        }        return value;    }    /**     * 获得邮件主题     */    public static String getSubject(Message mimeMessage) {        String subject = "";        try {//            subject = mimeMessage.getSubject();  //getSubject方法已执行了decode的,正确decode完的字符串执行"ISO8859_1"转"GBK反而会乱码;            if (mimeMessage.getHeader("subject") != null)                subject = mimeMessage.getHeader("subject")[0];            if (subject == null) {                subject = "";            } else {                subject = decodeHeader(subject);            }        } catch (Exception ex) {            Logger.getLogger(JavaMailFrame.class.getName()).log(Level.SEVERE, null, ex);        }        return subject;    }    /**     * 获得邮件发送日期     */    public static String getSentDate(Message mimeMessage) {        String strSentDate = "";        try {            Date sentDate = mimeMessage.getSentDate();            if (sentDate == null) {                sentDate = new Date();            }            SimpleDateFormat format = new SimpleDateFormat("yy年MM月dd日 HH:mm");            strSentDate =                    format.format(sentDate);        } catch (MessagingException ex) {            Logger.getLogger(JavaMailFrame.class.getName()).log(Level.SEVERE, null, ex);        }        return strSentDate;    }    public String getSize(Message mimeMessage) {        try {            return (mimeMessage.getSize() + 1000) / 1000 + "K";        } catch (MessagingException ex) {            Logger.getLogger(JavaMailFrame.class.getName()).log(Level.SEVERE, null, ex);        }        return "";    }    // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JButton btnRecv;    private javax.swing.JScrollPane jScrollPane1;    private javax.swing.JTable jTable1;    private javax.swing.JToolBar jToolBar1;    // End of variables declaration//GEN-END:variables}

⌨️ 快捷键说明

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