Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1997-2008 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.  
  32. import java.util.*;
  33. import javax.mail.*;
  34. import javax.mail.internet.*;
  35. import javax.mail.event.*;
  36. import javax.activation.*;
  37.  
  38. /**
  39.  * transport is a simple program that creates a message, explicitly
  40.  * retrieves a Transport from the session based on the type of the
  41.  * address (it's InternetAddress, so SMTP will be used) and sends
  42.  * the message.
  43.  *
  44.  * usage: <code>java transport <i>"toaddr1[, toaddr2]*"  from smtphost
  45.  * true|false</i></code><br>
  46.  * where <i>to</i> and <i>from</i> are the destination and
  47.  * origin email addresses, respectively, and <i>smtphost</i>
  48.  * is the hostname of the machine that has the smtp server
  49.  * running. The <i>to</i> addresses can be either a single email
  50.  * address or a comma-separated list of email addresses in
  51.  * quotes, i.e. "joe@machine, jane, max@server.com"
  52.  * The last parameter either turns on or turns off
  53.  * debugging during sending.
  54.  *
  55.  * @author Max Spivak
  56.  */
  57. public class transport implements ConnectionListener, TransportListener {
  58.     static String msgText = "This is a message body.\nHere's the second line.";
  59.     static String msgText2 = "\nThis was sent by transport.java demo program.";
  60.  
  61.     public static void main(String[] args) {
  62.         Properties props = System.getProperties();
  63.         // parse the arguments
  64.         InternetAddress[] addrs = null;
  65.         InternetAddress from;
  66.         boolean debug = false;
  67.         if (args.length != 4) {
  68.             usage();
  69.             return;
  70.         } else {
  71.             props.put("mail.smtp.host", args[2]);
  72.             if (args[3].equals("true")) {
  73.                 debug = true;
  74.             } else if (args[3].equals("false")) {
  75.                 debug = false;
  76.             } else {
  77.                 usage();
  78.                 return;
  79.             }
  80.  
  81.             // parse the destination addresses
  82.             try {
  83.                 addrs = InternetAddress.parse(args[0], false);
  84.                 from = new InternetAddress(args[1]);
  85.             } catch (AddressException aex) {
  86.                 System.out.println("Invalid Address");
  87.                 aex.printStackTrace();
  88.                 return;
  89.             }
  90.         }
  91.         // create some properties and get a Session
  92.         Session session = Session.getInstance(props, null);
  93.         session.setDebug(debug);
  94.  
  95.         transport t = new transport();
  96.         t.go(session, addrs, from);
  97.     }
  98.  
  99.     public transport() {}
  100.  
  101.     public void go(Session session, InternetAddress[] toAddr,
  102.                                 InternetAddress from) {
  103.         Transport trans = null;
  104.  
  105.         try {
  106.             // create a message
  107.             Message msg = new MimeMessage(session);
  108.             msg.setFrom(from);
  109.             msg.setRecipients(Message.RecipientType.TO, toAddr);
  110.             msg.setSubject("JavaMail APIs transport.java Test");
  111.             msg.setSentDate(new Date());  // Date: header
  112.             msg.setContent(msgText+msgText2, "text/plain");
  113.             msg.saveChanges();
  114.  
  115.             // get the smtp transport for the address
  116.             trans = session.getTransport(toAddr[0]);
  117.  
  118.             // register ourselves as listener for ConnectionEvents
  119.             // and TransportEvents
  120.             trans.addConnectionListener(this);
  121.             trans.addTransportListener(this);
  122.  
  123.             // connect the transport
  124.             trans.connect();
  125.  
  126.             // send the message
  127.             trans.sendMessage(msg, toAddr);
  128.  
  129.             // give the EventQueue enough time to fire its events
  130.             try {Thread.sleep(5);}catch(InterruptedException e) {}
  131.  
  132.         } catch (MessagingException mex) {
  133.             // give the EventQueue enough time to fire its events
  134.             try {Thread.sleep(5);}catch(InterruptedException e) {}
  135.  
  136.             System.out.println("Sending failed with exception:");
  137.             mex.printStackTrace();
  138.             System.out.println();
  139.             Exception ex = mex;
  140.             do {
  141.                 if (ex instanceof SendFailedException) {
  142.                     SendFailedException sfex = (SendFailedException)ex;
  143.                     Address[] invalid = sfex.getInvalidAddresses();
  144.                     if (invalid != null) {
  145.                         System.out.println("    ** Invalid Addresses");
  146.                         if (invalid != null) {
  147.                             for (int i = 0; i < invalid.length; i++)
  148.                                 System.out.println("         " + invalid[i]);
  149.                         }
  150.                     }
  151.                     Address[] validUnsent = sfex.getValidUnsentAddresses();
  152.                     if (validUnsent != null) {
  153.                         System.out.println("    ** ValidUnsent Addresses");
  154.                         if (validUnsent != null) {
  155.                             for (int i = 0; i < validUnsent.length; i++)
  156.                                 System.out.println("         "+validUnsent[i]);
  157.                         }
  158.                     }
  159.                     Address[] validSent = sfex.getValidSentAddresses();
  160.                     if (validSent != null) {
  161.                         System.out.println("    ** ValidSent Addresses");
  162.                         if (validSent != null) {
  163.                             for (int i = 0; i < validSent.length; i++)
  164.                                 System.out.println("         "+validSent[i]);
  165.                         }
  166.                     }
  167.                 }
  168.                 System.out.println();
  169.                 if (ex instanceof MessagingException)
  170.                     ex = ((MessagingException)ex).getNextException();
  171.                 else
  172.                     ex = null;
  173.             } while (ex != null);
  174.         } finally {
  175.             try {
  176.                 // close the transport
  177.                 if (trans != null)
  178.                     trans.close();
  179.             } catch (MessagingException mex) { /* ignore */ }
  180.         }
  181.     }
  182.  
  183.     // implement ConnectionListener interface
  184.     public void opened(ConnectionEvent e) {
  185.         System.out.println(">>> ConnectionListener.opened()");
  186.     }
  187.     public void disconnected(ConnectionEvent e) {}
  188.     public void closed(ConnectionEvent e) {
  189.         System.out.println(">>> ConnectionListener.closed()");
  190.     }
  191.  
  192.     // implement TransportListener interface
  193.     public void messageDelivered(TransportEvent e) {
  194.         System.out.println(">>> TransportListener.messageDelivered().");
  195.         System.out.println(" Valid Addresses:");
  196.         Address[] valid = e.getValidSentAddresses();
  197.         if (valid != null) {
  198.             for (int i = 0; i < valid.length; i++)
  199.                 System.out.println("    " + valid[i]);
  200.         }
  201.     }
  202.     public void messageNotDelivered(TransportEvent e) {
  203.         System.out.println(">>> TransportListener.messageNotDelivered().");
  204.         System.out.println(" Invalid Addresses:");
  205.         Address[] invalid = e.getInvalidAddresses();
  206.         if (invalid != null) {
  207.             for (int i = 0; i < invalid.length; i++)
  208.                 System.out.println("    " + invalid[i]);
  209.         }
  210.     }
  211.     public void messagePartiallyDelivered(TransportEvent e) {
  212.         System.out.println(">>> TransportListener.messagePartiallyDelivered().");
  213.         System.out.println(" Valid Addresses:");
  214.         Address[] valid = e.getValidSentAddresses();
  215.         if (valid != null) {
  216.             for (int i = 0; i < valid.length; i++)
  217.                 System.out.println("    " + valid[i]);
  218.         }
  219.         System.out.println(" Valid Unsent Addresses:");
  220.         Address[] unsent = e.getValidUnsentAddresses();
  221.         if (unsent != null) {
  222.             for (int i = 0; i < unsent.length; i++)
  223.                 System.out.println("    " + unsent[i]);
  224.         }
  225.         System.out.println(" Invalid Addresses:");
  226.         Address[] invalid = e.getInvalidAddresses();
  227.         if (invalid != null) {
  228.             for (int i = 0; i < invalid.length; i++)
  229.                 System.out.println("    " + invalid[i]);
  230.         }
  231.     }
  232.  
  233.     private static void usage() {
  234.         System.out.println(
  235.     "usage: java transport \"<to1>[, <to2>]*\" <from> <smtp> true|false");
  236.         System.out.println(
  237.     "example: java transport \"joe@machine, jane\" senderaddr smtphost false");
  238.     }
  239. }
  240.