📄 usefiltrate.java
字号:
import javax.mail.*;import javax.mail.search.*;import javax.mail.internet.*;import java.util.*;import java.io.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class useFiltrate{ public static void main(String[] args) { if (args.length == 0) { System.err.println( "Usage: java SearchClient protocol://username@host/foldername"); return; } //新建URLName对象 URLName server = new URLName(args[0]); try { Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator(server.getUsername())); // 连接邮件服务器,打开文件夹 Folder folder = session.getFolder(server); if (folder == null) { System.out.println("Folder " + server.getFile() + " not found."); System.exit(1); } folder.open(Folder.READ_ONLY); SearchTerm term = null; if (args.length > 1) { SearchTerm[] terms = new SearchTerm[args.length-1]; for (int i = 1; i < args.length; i++) { Address a = new InternetAddress(args[i]); terms[i-1] = new FromTerm(new InternetAddress(args[i])); } if (terms.length > 1) term = new OrTerm(terms); else term = terms[0]; } // 从URLName中获取信息 Message[] messages; if (term == null) { messages = folder.getMessages(); } else { messages = folder.search(term); } for (int i = 0; i < messages.length; i++) { System.out.println("------------ Message " + (i+1)+ " ------------"); // 打印信息头 Enumeration headers = messages[i].getAllHeaders(); while (headers.hasMoreElements()) { Header h = (Header) headers.nextElement(); System.out.println(h.getName() + ": " + h.getValue()); } System.out.println(); // 主体部分 Object body = messages[i].getContent(); if (body instanceof Multipart) { processMultipart((Multipart) body); } else { // 常见信息 processPart(messages[i]); } System.out.println(); } // 关闭连接 folder.close(false); } catch (Exception e) { e.printStackTrace(); } // 退出系统 System.exit(0); } //多部分处理 public static void processMultipart(Multipart mp) throws MessagingException { for (int i = 0; i < mp.getCount(); i++) { processPart(mp.getBodyPart(i)); } } //单独部分处理 public static void processPart(Part p) { try { String fileName = p.getFileName(); if (fileName == null) { // 内嵌 p.writeTo(System.out); } else if (fileName != null) { File f = new File(fileName); // 发现不存在的信息 for (int i = 1; f.exists(); i++) { String newName = fileName + " " + i; f = new File(newName); } FileOutputStream out = new FileOutputStream(f); //缓存输入流 InputStream in = new BufferedInputStream(p.getInputStream()); int b; while ((b = in.read()) != -1) out.write(b); //更新缓存 out.flush(); out.close(); in.close(); } } catch (Exception e) { System.err.println(e); e.printStackTrace(); } }}class MailAuthenticator extends Authenticator { //属性 private JDialog passwordDialog = new JDialog(new JFrame(), true); private JLabel mainLabel = new JLabel( "输入用户名和密码: "); private JLabel userLabel = new JLabel("用户名: "); private JLabel passwordLabel = new JLabel("密码: "); private JTextField usernameField = new JTextField(20); private JPasswordField passwordField = new JPasswordField(20); private JButton okButton = new JButton("确定"); //构造器 public MailAuthenticator() { this(""); } //带String参数的构造器 public MailAuthenticator(String username) { //容器 Container pane = passwordDialog.getContentPane(); //设置布局 pane.setLayout(new GridLayout(4, 1)); //添加标签 pane.add(mainLabel); //添加面板 JPanel p2 = new JPanel(); p2.add(userLabel); p2.add(usernameField); //设置文本内容 usernameField.setText(username); pane.add(p2); JPanel p3 = new JPanel(); p3.add(passwordLabel); p3.add(passwordField); pane.add(p3); JPanel p4 = new JPanel(); //添加按钮 p4.add(okButton); pane.add(p4); passwordDialog.pack(); ActionListener al = new HideDialog(); //监听按钮动作 okButton.addActionListener(al); usernameField.addActionListener(al); passwordField.addActionListener(al); } //HideDialog类 class HideDialog implements ActionListener { public void actionPerformed(ActionEvent e) { //隐藏对话框 passwordDialog.hide(); } } public PasswordAuthentication getPasswordAuthentication() { //显示对话框 passwordDialog.show(); // getPassword() 返回字符串 String password = new String(passwordField.getPassword()); String username = usernameField.getText(); //清除password passwordField.setText(""); //返回 return new PasswordAuthentication(username, password); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -