📄 mailreader.java~100~
字号:
package mailofjava;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import javax.mail.*;import javax.mail.internet.*;public class MailReader extends JFrame { 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 JTextArea jta=new JTextArea();; private JScrollPane jsp=new JScrollPane(); private Message msg[]; private String body; public MailReader() { try { CreateMessage cm=new CreateMessage(); msg=cm.getMessage() ;//取得邮件服务器消息 this.totalMail = msg.length ;//总邮件数 this.currentMail = 0; //第0封 this.attachCount =0;//附件个数 LayoutPrepare(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { MailReader mailReader = new MailReader(); mailReader.setSize(1000,600); mailReader.setVisible(true); mailReader.show(); }/** 准备进行布局设定和listener设定*/ private void LayoutPrepare() throws Exception { try{ this.setMailMessager(0) ; } catch(Exception e){System.out.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.out.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.out.println("currentmail/total"+currentMail+"/"+totalMail); if( (currentMail-1) >=0){ currentMail--; setMailMessager(currentMail); jp2.repaint() ;//强制 } else JOptionPane.showMessageDialog(null,"对不起,已经是头一个邮件!"); };//func } ); jpup.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.getViewport().add(jta);//加rollbar //jsp.setSize(100,100) ; jpdown.add(jta,BorderLayout.CENTER ); //jpdown.add(jsp,BorderLayout.CENTER ); jsp.getViewport().add(jpdown); jp1.add(jsp,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); this.setTitle("欢迎使用Mirthrandir的邮件阅读程序!"); this.getContentPane().setLayout(borderLayout1) ; this.getContentPane().add(jp1,BorderLayout.NORTH ); this.getContentPane().add(jp2,BorderLayout.SOUTH ); } public void setMailMessager(int thisMail){ //先清除上次的附件 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.out.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.out.println("Mime类型"+msg[thisMail].getContentType() ); if(msg[thisMail].isMimeType("multipart/*") ){ System.out.println("多部份处理开始"); Multipart mp= (Multipart) msg[thisMail].getContent() ; body=""; for(int i=0;i<mp.getCount() ;i++){ Part part= mp.getBodyPart(i) ; System.out.println("分部类型" +part.getContentType()); if(part.isMimeType("text/plain") ) body=body+(String) part.getContent() ; String disposition= part.getDisposition() ; if(disposition!=null && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE ))) { labelAttach[this.attachCount]= new JButton("本文的附件:"+part.getFileName() ); labelAttach[this.attachCount].setForeground(java.awt.Color.yellow); 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,"按了啊,可以存盘也!"); };//func } ); jp2.add(labelAttach[this.attachCount]); jp2.repaint() ; jp2.validate() ; this.attachCount ++; if(this.attachCount >=2) System.out.println("here"); };//if // if(part.isMimeType("text/html") ) // body=body+(String) part.getContent() ; }//for }//如果是多部份的就。。 jta.setText(body) ; }catch(Exception e){ e.printStackTrace() ;}; }//method}//class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -