1 38 import java.util.*; 39 40 import javax.mail.*; 41 import javax.mail.internet.*; 42 43 52 53 public class registry { 54 static Provider _aProvider, _bProvider, _sunSMTP, _sunIMAP; 56 57 public static void main(String [] args) { 58 Properties props = new Properties(); 59 60 props.put("mail.transport.protocol", "smtp"); 63 props.put("mail.store.protocol", "imap"); 64 65 props.put("mail.smtp.class", "com.sun.mail.smtp.SMTPTransport"); 67 props.put("mail.imap.class", "com.sun.mail.imap.IMAPStore"); 68 69 Session session = Session.getInstance(props, null); 70 72 System.out.println("\n------ getProviders()----------"); 74 Provider[] providers = session.getProviders(); 75 for (int i = 0; i < providers.length; i++) { 76 System.out.println("** " + providers[i]); 77 78 String s = null; 83 if (((s = providers[i].getVendor()) != null) && 84 s.startsWith("ACME Corp")) { 85 _aProvider = providers[i]; 86 } 87 88 if (providers[i].getClassName().endsWith("application.smtp")) 90 _bProvider = providers[i]; 91 92 if (providers[i].getClassName().equals("com.sun.mail.imap.IMAPStore")){ 95 _sunIMAP = providers[i]; 96 } 97 98 if (((s = providers[i].getVendor()) != null) && 102 s.startsWith("Sun Microsystems") && 103 providers[i].getType() == Provider.Type.TRANSPORT && 104 providers[i].getProtocol().equalsIgnoreCase("smtp")) { 105 _sunSMTP = providers[i]; 106 } 107 } 108 109 System.out.println("\n------ initial protocol defaults -------"); 110 try { 111 System.out.println("imap: " + session.getProvider("imap")); 112 System.out.println("smtp: " + session.getProvider("smtp")); 113 System.out.println("nntp: " + session.getProvider("nntp")); 115 } catch (NoSuchProviderException mex) { 116 System.out.println(">> This exception is OK since there is no NNTP Provider configured by default"); 117 mex.printStackTrace(); 118 } 119 120 System.out.println("\n------ set new protocol defaults ------"); 121 try { 123 session.setProvider(_aProvider); } catch (NoSuchProviderException mex) { 126 System.out.println(">> Exception expected: _aProvider is null"); 127 mex.printStackTrace(); 128 } 129 try { 130 session.setProvider(_sunIMAP); 132 } catch (NoSuchProviderException mex) { mex.printStackTrace(); } 133 try { 134 System.out.println("imap: " + session.getProvider("imap")); 135 System.out.println("smtp: " + session.getProvider("smtp")); 136 } catch (NoSuchProviderException mex) { mex.printStackTrace(); } 137 138 139 System.out.println("\n\n----- get some stores ---------"); 140 try { 143 System.out.println("getStore(): " + session.getStore()); 144 System.out.println("getStore(Provider): " + 145 session.getStore(_sunIMAP)); 146 } catch (NoSuchProviderException mex) { 147 mex.printStackTrace(); 148 } 149 150 try { 151 System.out.println("getStore(imap): " + session.getStore("imap")); 152 System.out.println("getStore(pop3): " + session.getStore("pop3")); 154 } catch (NoSuchProviderException mex) { 155 System.out.println(">> Exception expected: no pop3 provider"); 156 mex.printStackTrace(); 157 } 158 159 160 System.out.println("\n\n----- now for transports/addresses ---------"); 161 try { 163 System.out.println("getTransport(): " + session.getTransport()); 164 System.out.println("getTransport(Provider): " + 165 session.getTransport(_sunSMTP)); 166 System.out.println("getTransport(smtp): " + 167 session.getTransport("smtp")); 168 System.out.println("getTransport(Address): " + 169 session.getTransport(new InternetAddress("mspivak@apilon"))); 170 System.out.println("getTransport(News): " + 172 session.getTransport(new NewsAddress("rec.humor"))); 173 } catch (MessagingException mex) { 174 System.out.println(">> Exception expected: no news provider configured"); 175 mex.printStackTrace(); 176 } 177 } 178 } 179 | Popular Tags |