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.io.*;
  33. import java.util.Properties;
  34. import java.util.Date;
  35.  
  36. import javax.mail.*;
  37. import javax.mail.internet.*;
  38.  
  39. /**
  40.  * Demo app that shows how to construct and send an RFC822
  41.  * (singlepart) message.
  42.  *
  43.  * XXX - allow more than one recipient on the command line
  44.  *
  45.  * @author Max Spivak
  46.  * @author Bill Shannon
  47.  */
  48.  
  49. public class msgsend {
  50.  
  51.     public static void main(String[] argv) {
  52.         String  to, subject = null, from = null,
  53.                 cc = null, bcc = null, url = null;
  54.         String mailhost = null;
  55.         String mailer = "msgsend";
  56.         String file = null;
  57.         String protocol = null, host = null, user = null, password = null;
  58.         String record = null;   // name of folder in which to record mail
  59.         boolean debug = false;
  60.         BufferedReader in =
  61.                         new BufferedReader(new InputStreamReader(System.in));
  62.         int optind;
  63.  
  64.         /*
  65.          * Process command line arguments.
  66.          */
  67.         for (optind = 0; optind < argv.length; optind++) {
  68.             if (argv[optind].equals("-T")) {
  69.                 protocol = argv[++optind];
  70.             } else if (argv[optind].equals("-H")) {
  71.                 host = argv[++optind];
  72.             } else if (argv[optind].equals("-U")) {
  73.                 user = argv[++optind];
  74.             } else if (argv[optind].equals("-P")) {
  75.                 password = argv[++optind];
  76.             } else if (argv[optind].equals("-M")) {
  77.                 mailhost = argv[++optind];
  78.             } else if (argv[optind].equals("-f")) {
  79.                 record = argv[++optind];
  80.             } else if (argv[optind].equals("-a")) {
  81.                 file = argv[++optind];
  82.             } else if (argv[optind].equals("-s")) {
  83.                 subject = argv[++optind];
  84.             } else if (argv[optind].equals("-o")) { // originator
  85.                 from = argv[++optind];
  86.             } else if (argv[optind].equals("-c")) {
  87.                 cc = argv[++optind];
  88.             } else if (argv[optind].equals("-b")) {
  89.                 bcc = argv[++optind];
  90.             } else if (argv[optind].equals("-L")) {
  91.                 url = argv[++optind];
  92.             } else if (argv[optind].equals("-d")) {
  93.                 debug = true;
  94.             } else if (argv[optind].equals("--")) {
  95.                 optind++;
  96.                 break;
  97.             } else if (argv[optind].startsWith("-")) {
  98.                 System.out.println(
  99. "Usage: msgsend [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
  100.                 System.out.println(
  101. "\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]");
  102.                 System.out.println(
  103. "\t[-f record-mailbox] [-M transport-host] [-a attach-file] [-d] [address]");
  104.                 System.exit(1);
  105.             } else {
  106.                 break;
  107.             }
  108.         }
  109.  
  110.         try {
  111.             /*
  112.              * Prompt for To and Subject, if not specified.
  113.              */
  114.             if (optind < argv.length) {
  115.                 // XXX - concatenate all remaining arguments
  116.                 to = argv[optind];
  117.                 System.out.println("To: " + to);
  118.             } else {
  119.                 System.out.print("To: ");
  120.                 System.out.flush();
  121.                 to = in.readLine();
  122.             }
  123.             if (subject == null) {
  124.                 System.out.print("Subject: ");
  125.                 System.out.flush();
  126.                 subject = in.readLine();
  127.             } else {
  128.                 System.out.println("Subject: " + subject);
  129.             }
  130.  
  131.             /*
  132.              * Initialize the JavaMail Session.
  133.              */
  134.             Properties props = System.getProperties();
  135.             // XXX - could use Session.getTransport() and Transport.connect()
  136.             // XXX - assume we're using SMTP
  137.             if (mailhost != null)
  138.                 props.put("mail.smtp.host", mailhost);
  139.  
  140.             // Get a Session object
  141.             Session session = Session.getInstance(props, null);
  142.             if (debug)
  143.                 session.setDebug(true);
  144.  
  145.             /*
  146.              * Construct the message and send it.
  147.              */
  148.             Message msg = new MimeMessage(session);
  149.             if (from != null)
  150.                 msg.setFrom(new InternetAddress(from));
  151.             else
  152.                 msg.setFrom();
  153.  
  154.             msg.setRecipients(Message.RecipientType.TO,
  155.                                         InternetAddress.parse(to, false));
  156.             if (cc != null)
  157.                 msg.setRecipients(Message.RecipientType.CC,
  158.                                         InternetAddress.parse(cc, false));
  159.             if (bcc != null)
  160.                 msg.setRecipients(Message.RecipientType.BCC,
  161.                                         InternetAddress.parse(bcc, false));
  162.  
  163.             msg.setSubject(subject);
  164.  
  165.             String text = collect(in);
  166.  
  167.             if (file != null) {
  168.                 // Attach the specified file.
  169.                 // We need a multipart message to hold the attachment.
  170.                 MimeBodyPart mbp1 = new MimeBodyPart();
  171.                 mbp1.setText(text);
  172.                 MimeBodyPart mbp2 = new MimeBodyPart();
  173.                 mbp2.attachFile(file);
  174.                 MimeMultipart mp = new MimeMultipart();
  175.                 mp.addBodyPart(mbp1);
  176.                 mp.addBodyPart(mbp2);
  177.                 msg.setContent(mp);
  178.             } else {
  179.                 // If the desired charset is known, you can use
  180.                 // setText(text, charset)
  181.                 msg.setText(text);
  182.             }
  183.  
  184.             msg.setHeader("X-Mailer", mailer);
  185.             msg.setSentDate(new Date());
  186.  
  187.             // send the thing off
  188.             Transport.send(msg);
  189.  
  190.             System.out.println("\nMail was sent successfully.");
  191.  
  192.             /*
  193.              * Save a copy of the message, if requested.
  194.              */
  195.             if (record != null) {
  196.                 // Get a Store object
  197.                 Store store = null;
  198.                 if (url != null) {
  199.                     URLName urln = new URLName(url);
  200.                     store = session.getStore(urln);
  201.                     store.connect();
  202.                 } else {
  203.                     if (protocol != null)              
  204.                         store = session.getStore(protocol);
  205.                     else
  206.                         store = session.getStore();
  207.  
  208.                     // Connect
  209.                     if (host != null || user != null || password != null)
  210.                         store.connect(host, user, password);
  211.                     else
  212.                         store.connect();
  213.                 }
  214.  
  215.                 // Get record Folder.  Create if it does not exist.
  216.                 Folder folder = store.getFolder(record);
  217.                 if (folder == null) {
  218.                     System.err.println("Can't get record folder.");
  219.                     System.exit(1);
  220.                 }
  221.                 if (!folder.exists())
  222.                     folder.create(Folder.HOLDS_MESSAGES);
  223.  
  224.                 Message[] msgs = new Message[1];
  225.                 msgs[0] = msg;
  226.                 folder.appendMessages(msgs);
  227.  
  228.                 System.out.println("Mail was recorded successfully.");
  229.             }
  230.  
  231.         } catch (Exception e) {
  232.             e.printStackTrace();
  233.         }
  234.     }
  235.  
  236.     /**
  237.      * Read the body of the message until EOF.
  238.      */
  239.     public static String collect(BufferedReader in) throws IOException {
  240.         String line;
  241.         StringBuffer sb = new StringBuffer();
  242.         while ((line = in.readLine()) != null) {
  243.             sb.append(line);
  244.             sb.append("\n");
  245.         }
  246.         return sb.toString();
  247.     }
  248. }
  249.