Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31. import java.util.*;
  32.  
  33. import javax.mail.*;
  34. import javax.mail.internet.*;
  35.  
  36. /**
  37.  * This class demonstrates how to query the registry for available
  38.  * Providers, set default providers, etc. See section 5.2 in the
  39.  * JavaMail 1.0 Spec for details on how to use the registry.
  40.  *
  41.  * See the comments inline for what's happening.
  42.  *
  43.  * @author Max Spivak
  44.  */
  45.  
  46. public class registry {
  47.     // let's remember a few providers
  48.     static Provider _aProvider, _bProvider, _sunSMTP, _sunIMAP;
  49.  
  50.     public static void main(String[] args) {
  51.         Properties props = new Properties();
  52.  
  53.         // set smtp and imap to be our default
  54.         // transport and store protocols, respectively
  55.         props.put("mail.transport.protocol", "smtp");
  56.         props.put("mail.store.protocol", "imap");
  57.  
  58.         //
  59.         props.put("mail.smtp.class", "com.sun.mail.smtp.SMTPTransport");
  60.         props.put("mail.imap.class", "com.sun.mail.imap.IMAPStore");
  61.        
  62.         Session session = Session.getInstance(props, null);
  63.         //session.setDebug(true);
  64.  
  65.         // Retrieve all configured providers from the Session
  66.         System.out.println("\n------ getProviders()----------");
  67.         Provider[] providers = session.getProviders();
  68.         for (int i = 0; i < providers.length; i++) {
  69.             System.out.println("** " + providers[i]);
  70.  
  71.             // let's remember some providers so that we can use them later
  72.             // (I'm explicitly showing multiple ways of testing Providers)
  73.             // BTW, no Provider "ACME Corp" will be found in the default
  74.             // setup b/c its not in any javamail.providers resource files
  75.             String s = null;
  76.             if (((s = providers[i].getVendor()) != null) &&
  77.                 s.startsWith("ACME Corp")) {
  78.                 _aProvider = providers[i];
  79.             }
  80.  
  81.             // this Provider won't be found by default either
  82.             if (providers[i].getClassName().endsWith("application.smtp"))
  83.                 _bProvider = providers[i];
  84.  
  85.             // this Provider will be found since com.sun.mail.imap.IMAPStore
  86.             // is configured in javamail.default.providers
  87.             if (providers[i].getClassName().equals("com.sun.mail.imap.IMAPStore")){
  88.                 _sunIMAP = providers[i];
  89.             }
  90.  
  91.             // this Provider will be found as well since there is a
  92.             // Sun Microsystems SMTP transport configured by
  93.             // default in javamail.default.providers
  94.             if (((s = providers[i].getVendor()) != null) &&
  95.                 s.startsWith("Sun Microsystems") &&
  96.                 providers[i].getType() == Provider.Type.TRANSPORT &&
  97.                 providers[i].getProtocol().equalsIgnoreCase("smtp")) {
  98.                 _sunSMTP = providers[i];
  99.             }
  100.         }
  101.        
  102.         System.out.println("\n------ initial protocol defaults -------");
  103.         try {
  104.             System.out.println("imap: " + session.getProvider("imap"));
  105.             System.out.println("smtp: " + session.getProvider("smtp"));
  106.             // the NNTP provider will fail since we don't have one configured
  107.             System.out.println("nntp: " + session.getProvider("nntp"));
  108.         } catch (NoSuchProviderException mex) {
  109.             System.out.println(">> This exception is OK since there is no NNTP Provider configured by default");
  110.             mex.printStackTrace();
  111.         }
  112.  
  113.         System.out.println("\n------ set new protocol defaults ------");
  114.         // set some new defaults
  115.         try {
  116.             // since _aProvider isn't configured, this will fail
  117.             session.setProvider(_aProvider);  // will fail
  118.         } catch (NoSuchProviderException mex) {
  119.             System.out.println(">> Exception expected: _aProvider is null");
  120.             mex.printStackTrace();
  121.         }
  122.         try {
  123.             // _sunIMAP provider should've configured correctly; should work
  124.             session.setProvider(_sunIMAP);
  125.         } catch (NoSuchProviderException mex) { mex.printStackTrace(); }
  126.         try {
  127.             System.out.println("imap: " + session.getProvider("imap"));
  128.             System.out.println("smtp: " + session.getProvider("smtp"));
  129.         } catch (NoSuchProviderException mex) { mex.printStackTrace(); }
  130.  
  131.  
  132.         System.out.println("\n\n----- get some stores ---------");
  133.         // multiple ways to retrieve stores. these will print out the
  134.         // string "imap:" since its the URLName for the store
  135.         try {
  136.             System.out.println("getStore(): " + session.getStore());
  137.             System.out.println("getStore(Provider): " +
  138.                                session.getStore(_sunIMAP));
  139.         } catch (NoSuchProviderException mex) {
  140.             mex.printStackTrace();
  141.         }
  142.  
  143.         try {
  144.             System.out.println("getStore(imap): " + session.getStore("imap"));
  145.             // pop3 will fail since it doesn't exist
  146.             System.out.println("getStore(pop3): " + session.getStore("pop3"));
  147.         } catch (NoSuchProviderException mex) {
  148.             System.out.println(">> Exception expected: no pop3 provider");
  149.             mex.printStackTrace();
  150.         }
  151.  
  152.  
  153.         System.out.println("\n\n----- now for transports/addresses ---------");
  154.         // retrieve transports; these will print out "smtp:" (like stores did)
  155.         try {
  156.             System.out.println("getTransport(): " + session.getTransport());
  157.             System.out.println("getTransport(Provider): " +
  158.                                session.getTransport(_sunSMTP));
  159.             System.out.println("getTransport(smtp): " +
  160.                                session.getTransport("smtp"));
  161.             System.out.println("getTransport(Address): " +
  162.                                session.getTransport(new InternetAddress("mspivak@apilon")));
  163.             // News will fail since there's no news provider configured
  164.             System.out.println("getTransport(News): " +
  165.                                session.getTransport(new NewsAddress("rec.humor")));
  166.         } catch (MessagingException mex) {
  167.             System.out.println(">> Exception expected: no news provider configured");
  168.             mex.printStackTrace();
  169.         }
  170.     }
  171. }
  172.