1 6 7 import java.io.*; 8 import java.net.*; 9 import javax.mail.*; 10 import javax.mail.PasswordAuthentication ; 11 import javax.mail.Authenticator ; 12 13 26 27 public class TtyAuthenticator extends Authenticator { 28 29 33 protected PasswordAuthentication getPasswordAuthentication() { 34 BufferedReader in = new BufferedReader( 35 new InputStreamReader((System.in))); 36 StringBuffer sb = new StringBuffer (); 37 sb.append("Connecting to "); 38 sb.append(getRequestingProtocol()); 39 sb.append(" mail service on host "); 40 sb.append(getRequestingSite().getHostName()); 41 int port = getRequestingPort(); 42 if (port > 0) { 43 sb.append(", port "); 44 sb.append(port); 45 } 46 sb.append("."); 47 System.out.println(sb.toString()); 48 String prompt = getRequestingPrompt(); 49 if (prompt != null) 50 System.out.println(prompt); 51 System.out.println(); 52 String userName = get(in, "User Name", getDefaultUserName()); 53 String password = get(in, "Password", null); 54 if (userName == null) 55 return null; 56 else 57 return new PasswordAuthentication (userName, password); 58 } 59 60 private static final String get(BufferedReader in, 61 String name, String value) { 62 PrintStream p = System.out; 63 64 p.print(name + ": "); 65 if (value != null) 66 p.print("[" + value + "] "); 67 p.flush(); 68 69 try { 70 String s = in.readLine(); 71 if (s.length() == 0) 72 return value; 73 else 74 return s; 75 } catch (IOException e) { 76 return value; 77 } 78 } 79 80 public static void main(String argv[]) throws Exception { 83 Session sess = Session.getInstance(System.getProperties(), 84 new TtyAuthenticator()); 85 PasswordAuthentication pw = sess.requestPasswordAuthentication( 86 InetAddress.getByName(argv[0]), 87 Integer.parseInt(argv[1]), argv[2], z(argv[3]), z(argv[4])); 88 System.out.println("User: " + n(pw.getUserName())); 89 System.out.println("Password: " + n(pw.getPassword())); 90 } 91 92 private static final String n(String s) { 93 return s == null ? "<null>" : s; 94 } 95 96 private static final String z(String s) { 97 return s.length() > 0 ? s : null; 98 } 99 } 100 | Popular Tags |