Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1996-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.  
  32. import java.util.*;
  33. import java.io.*;
  34. import javax.mail.*;
  35. import javax.mail.internet.*;
  36. import javax.activation.*;
  37.  
  38. /**
  39.  * msgsendsample creates a very simple text/plain message and sends it.
  40.  * <p>
  41.  * usage: <code>java msgsendsample <i>to from smtphost true|false</i></code>
  42.  * where <i>to</i> and <i>from</i> are the destination and
  43.  * origin email addresses, respectively, and <i>smtphost</i>
  44.  * is the hostname of the machine that has the smtp server
  45.  * running. The last parameter either turns on or turns off
  46.  * debugging during sending.
  47.  *
  48.  * @author Max Spivak
  49.  */
  50. public class msgsendsample {
  51.     static String msgText = "This is a message body.\nHere's the second line.";
  52.  
  53.     public static void main(String[] args) {
  54.         if (args.length != 4) {
  55.             usage();
  56.             System.exit(1);
  57.         }
  58.  
  59.         System.out.println();
  60.        
  61.         String to = args[0];
  62.         String from = args[1];
  63.         String host = args[2];
  64.         boolean debug = Boolean.valueOf(args[3]).booleanValue();
  65.  
  66.         // create some properties and get the default Session
  67.         Properties props = new Properties();
  68.         props.put("mail.smtp.host", host);
  69.         if (debug) props.put("mail.debug", args[3]);
  70.  
  71.         Session session = Session.getInstance(props, null);
  72.         session.setDebug(debug);
  73.        
  74.         try {
  75.             // create a message
  76.             Message msg = new MimeMessage(session);
  77.             msg.setFrom(new InternetAddress(from));
  78.             InternetAddress[] address = {new InternetAddress(args[0])};
  79.             msg.setRecipients(Message.RecipientType.TO, address);
  80.             msg.setSubject("JavaMail APIs Test");
  81.             msg.setSentDate(new Date());
  82.             // If the desired charset is known, you can use
  83.             // setText(text, charset)
  84.             msg.setText(msgText);
  85.            
  86.             Transport.send(msg);
  87.         } catch (MessagingException mex) {
  88.             System.out.println("\n--Exception handling in msgsendsample.java");
  89.  
  90.             mex.printStackTrace();
  91.             System.out.println();
  92.             Exception ex = mex;
  93.             do {
  94.                 if (ex instanceof SendFailedException) {
  95.                     SendFailedException sfex = (SendFailedException)ex;
  96.                     Address[] invalid = sfex.getInvalidAddresses();
  97.                     if (invalid != null) {
  98.                         System.out.println("    ** Invalid Addresses");
  99.                         if (invalid != null) {
  100.                             for (int i = 0; i < invalid.length; i++)
  101.                                 System.out.println("         " + invalid[i]);
  102.                         }
  103.                     }
  104.                     Address[] validUnsent = sfex.getValidUnsentAddresses();
  105.                     if (validUnsent != null) {
  106.                         System.out.println("    ** ValidUnsent Addresses");
  107.                         if (validUnsent != null) {
  108.                             for (int i = 0; i < validUnsent.length; i++)
  109.                                 System.out.println("         "+validUnsent[i]);
  110.                         }
  111.                     }
  112.                     Address[] validSent = sfex.getValidSentAddresses();
  113.                     if (validSent != null) {
  114.                         System.out.println("    ** ValidSent Addresses");
  115.                         if (validSent != null) {
  116.                             for (int i = 0; i < validSent.length; i++)
  117.                                 System.out.println("         "+validSent[i]);
  118.                         }
  119.                     }
  120.                 }
  121.                 System.out.println();
  122.                 if (ex instanceof MessagingException)
  123.                     ex = ((MessagingException)ex).getNextException();
  124.                 else
  125.                     ex = null;
  126.             } while (ex != null);
  127.         }
  128.     }
  129.  
  130.     private static void usage() {
  131.         System.out.println("usage: java msgsendsample <to> <from> <smtp> true|false");
  132.     }
  133. }
  134.