📄 mailreader.java~217~
字号:
/** 综合运用mail的各个东西,包括显示复合的附件和图片*/package mailofjava;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;import java.io.*;/** 本程序是JFrame,可以用上一个下一个翻看邮件。请和RemoteMailManagement合用,代码复用的*/public class MailReader extends JFrame { private Stack attachementNameStack=new Stack(); private Stack attachementLabelStack = new Stack(); private BorderLayout borderLayout1 = new BorderLayout(); private JPanel jp1=new JPanel(new BorderLayout()); private JPanel jpup=new JPanel(new GridLayout(5,2)); private JPanel jpdown=new JPanel(new BorderLayout()); private JButton[] labelAttach=new JButton[20]; private JPanel jp2=new JPanel(new FlowLayout()); private JButton buttonExit= new JButton("退出"); private JButton buttonAbout= new JButton("关于作者"); private JButton buttonNext=new JButton("下一邮件"); private JButton buttonPrevious=new JButton("上一邮件"); private int totalMail; private int currentMail; private int attachCount; private JTextField textSubject=new JTextField("这里是mail subject"); private JLabel labelSubject=new JLabel("邮件标题"); private JTextField textFrom=new JTextField("这里是mail 来源"); private JLabel labelFrom=new JLabel("发件人"); private JTextField textTo=new JTextField("这里是mail 目的"); private JLabel labelTo=new JLabel("收件人"); private JTextField textDate=new JTextField("这里是mail 的日期"); private JLabel labelDate=new JLabel("日期"); private JEditorPane jta=new JEditorPane(); private JScrollPane jsp;; private JScrollPane jsp_edit; private Message msg[]; private String body; /** 构造。从服务器取得一堆的邮件,然后进行布局设定*/ public MailReader() { try { jta.setSize(600,600); CreateMessage cm=new CreateMessage(); msg=cm.getMessage() ;//取得邮件服务器消息 this.totalMail = msg.length ;//总邮件数 this.currentMail = 0; //第0封 this.attachCount =0;//附件个数 LayoutPrepare(); } catch(Exception e) { e.printStackTrace(); } }/** main函数,直接new 一个JFrame */ public static void main(String[] args) { MailReader mailReader = new MailReader(); mailReader.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); mailReader.setSize(1000,700); mailReader.setVisible(true); mailReader.show();}/** 准备进行布局设定和listener设定*/ private void LayoutPrepare() throws Exception { try{ this.setMailMessager(0) ; } catch(Exception e){System.err.println("err");}; buttonExit.addActionListener( new ActionListener(){ public void actionPerformed (ActionEvent ae){ System.exit(0); }; } ); buttonAbout.addActionListener( new ActionListener(){ public void actionPerformed (ActionEvent ae){ JOptionPane.showMessageDialog(null,"java高级教程mail篇 svc 曾海"); }; } ); buttonNext.addActionListener( new ActionListener(){ public void actionPerformed (ActionEvent ae){ //下一个邮件 System.err.println("currentmail/total"+currentMail+"/"+totalMail); if( (currentMail+1) < totalMail){ currentMail++; setMailMessager(currentMail); jp2.validate(); } else JOptionPane.showMessageDialog(null,"对不起,已经没得邮件也!"); };//func } ); buttonPrevious.addActionListener( new ActionListener(){ public void actionPerformed (ActionEvent ae){ //上一个邮件 System.err.println("currentmail/total"+currentMail+"/"+totalMail); if( (currentMail-1) >=0){ currentMail--; setMailMessager(currentMail); jp2.repaint() ;//强制 } else JOptionPane.showMessageDialog(null,"对不起,已经是头一个邮件!"); };//func } );// 最大是jp1:borderlayout //jpup: 是grid --> jp1.north //jpdown: 是border-->jp1.center //jp2: flowlayout -->jp1.southjpup.add(labelSubject); jpup.add(textSubject); jpup.add(labelFrom); jpup.add(textFrom); jpup.add(labelTo); jpup.add(textTo); jpup.add(labelDate); jpup.add(textDate); jp1.add(jpup,BorderLayout.NORTH);//顶部的头 jsp_edit=new JScrollPane(jta); //jsp_edit.setSize(500,400); jsp_edit.setHorizontalScrollBar(new JScrollBar(0)); jsp_edit.setVerticalScrollBar(new JScrollBar(1)); jsp_edit.setMaximumSize(new Dimension(600,400)); jsp_edit.setMinimumSize(new Dimension(600,400)); jp1.add(jsp_edit,BorderLayout.CENTER ); jp1.setBackground(java.awt.Color.darkGray ); jp2.setBackground(java.awt.Color.blue ); jp2.add(buttonExit); jp2.add(buttonAbout); jp2.add(buttonNext); jp2.add(buttonPrevious); jp1.add(jp2,BorderLayout.SOUTH ); this.setTitle("欢迎使用Mirthrandir的邮件阅读程序!"); this.getContentPane().setLayout(borderLayout1) ; this.getContentPane().add(jp1,BorderLayout.NORTH ); } public void setMailMessager(int thisMail){ //先清除上次的附件按钮 jta.setContentType("text/plain");//default if(this.attachCount !=0){ for(int i=0;i<attachCount;i++){ jp2.remove(labelAttach[i]); labelAttach[i]=null; }//for jp2.repaint(); jp2.validate();//key } attachCount=0;if(msg.length ==0){ System.err.println("没有新邮件"); System.exit(0); } try{ textSubject.setText( msg[thisMail].getSubject()) ; textDate.setText(msg[thisMail].getSentDate().toString() ); textFrom.setText(msg[thisMail].getFrom()[0].toString() ) ;//取第一个邮件地址 textTo.setText(msg[thisMail].getRecipients(Message.RecipientType.TO)[0].toString() ) ; Object obj = msg[thisMail].getContent() ; System.err.println("邮件总类型"+msg[thisMail].getContentType() ); System.err.println("==========================================="); if(msg[thisMail].isMimeType("multipart/*") ){//可能是mixed/alternative System.err.println("附件处理开始"); Multipart mp= (Multipart) msg[thisMail].getContent() ; body=""; for(int i=0;i<mp.getCount() ;i++){ Part part= mp.getBodyPart(i) ; System.err.println("FirstLevel:第"+i+"分部类型" +part.getContentType()); if(part.isMimeType("text/plain") ) body=body+(String) part.getContent() ; else if(part.isMimeType("text/html") ){ body=(String) part.getContent() ; jta.setContentType("text/plain"); }//html则冲掉原来的,试验html里有 !doctype,meta就统统显示不出来,痛苦啊! else if(part.isMimeType("multipart/alternative") )//20日修改 //part:取得第i部份的附件 { System.err.println("开始输出alternative的内容"); //其实它是由text/plain+ text/html构成 Multipart mp1 =(Multipart ) part.getContent() ; for(int k=0;k<mp1.getCount() ;k++){ BodyPart bp1 = mp1.getBodyPart(k);//第k分部的 System.err.println("alternavtive:"+k+"****"); //bp1.writeTo(System.err); System.err.println("********************************"); System.err.println("bp"+k+"类型"+ bp1.getContentType() ); if( bp1.isMimeType("text/plain") ) body=body+ (String) (bp1.getContent()) ; if(bp1.isMimeType("text/html") ) body+=(String) (bp1.getContent()); }//for System.err.println("alternative部份的内容是");// System.err.println(body); } /*是附件部份才会执行。*/ String disposition= part.getDisposition() ; if(disposition!=null && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE ))) { System.err.println(" 成功取出一个附件,正在处理。。"); labelAttach[this.attachCount]= new JButton("本文的附件:"+part.getFileName() ); labelAttach[this.attachCount].setForeground(java.awt.Color.yellow); labelAttach[this.attachCount].setBackground(java.awt.Color.blue); labelAttach[this.attachCount].setFont(new Font("宋体",Font.BOLD|Font.ITALIC,12 )); labelAttach[this.attachCount].addActionListener( new ActionListener(){ public void actionPerformed (ActionEvent ae){ JOptionPane.showMessageDialog(null,"按了啊,可以存盘也!"); //saveThisAttachment(); };//func } ); String name= part.getFileName(); this.attachementNameStack.push(name);//压入 FileOutputStream fos=new FileOutputStream( name);//新建文件存在documentbase上 BufferedInputStream in = new BufferedInputStream(part.getInputStream() ); int c; while((c=in.read() )!=-1)//直接用writeto什么东西也没有 fos.write(c); in.close() ; fos.close() ; fos=null; Icon iic= new ImageIcon(name);//取文件 JLabel jla=new JLabel(name,iic,SwingConstants.CENTER ); this.attachementLabelStack.add(jla);//压入// jp2.add(labelAttach[this.attachCount]); jp2.repaint() ; jp2.validate() ; this.attachCount ++; };//if }//for }//如果是多部份的就。。multipart //这两个else 是对付单部份文件的 else if (msg[thisMail].isMimeType("text/plain") ){ body=(String)(msg[thisMail].getContent() ); jta.setContentType("text/html"); }//else else if (msg[thisMail].isMimeType("text/html") ){ body=(String)(msg[thisMail].getContent() ); jta.setContentType("text/html"); }//else jta.setText(body) ; }catch(Exception e){ e.printStackTrace() ;}; this.showAttachedImages() ;//所有附件显示一下 }//methodpublic void showAttachedImages(){ while(!(attachementNameStack.isEmpty()) ) { String name=(String ) this.attachementNameStack.pop(); JLabel jla=(JLabel) this.attachementLabelStack.pop() ; jp2.add(jla); }//while}}//class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -