Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

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