0N/A/*
292N/A * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
292N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.Properties;
0N/Aimport java.util.Date;
0N/A
0N/Aimport javax.mail.*;
0N/Aimport javax.mail.internet.*;
0N/A
0N/A/**
0N/A * Demo app that shows how to construct and send an RFC822
0N/A * (singlepart) message.
0N/A *
0N/A * XXX - allow more than one recipient on the command line
0N/A *
0N/A * @author Max Spivak
0N/A * @author Bill Shannon
0N/A */
0N/A
0N/Apublic class msgsend {
0N/A
0N/A public static void main(String[] argv) {
0N/A String to, subject = null, from = null,
0N/A cc = null, bcc = null, url = null;
0N/A String mailhost = null;
0N/A String mailer = "msgsend";
0N/A String file = null;
0N/A String protocol = null, host = null, user = null, password = null;
0N/A String record = null; // name of folder in which to record mail
0N/A boolean debug = false;
0N/A BufferedReader in =
0N/A new BufferedReader(new InputStreamReader(System.in));
0N/A int optind;
0N/A
97N/A /*
97N/A * Process command line arguments.
97N/A */
0N/A for (optind = 0; optind < argv.length; optind++) {
0N/A if (argv[optind].equals("-T")) {
0N/A protocol = argv[++optind];
0N/A } else if (argv[optind].equals("-H")) {
0N/A host = argv[++optind];
0N/A } else if (argv[optind].equals("-U")) {
0N/A user = argv[++optind];
0N/A } else if (argv[optind].equals("-P")) {
0N/A password = argv[++optind];
0N/A } else if (argv[optind].equals("-M")) {
0N/A mailhost = argv[++optind];
0N/A } else if (argv[optind].equals("-f")) {
0N/A record = argv[++optind];
0N/A } else if (argv[optind].equals("-a")) {
0N/A file = argv[++optind];
0N/A } else if (argv[optind].equals("-s")) {
0N/A subject = argv[++optind];
0N/A } else if (argv[optind].equals("-o")) { // originator
0N/A from = argv[++optind];
0N/A } else if (argv[optind].equals("-c")) {
0N/A cc = argv[++optind];
0N/A } else if (argv[optind].equals("-b")) {
0N/A bcc = argv[++optind];
0N/A } else if (argv[optind].equals("-L")) {
0N/A url = argv[++optind];
0N/A } else if (argv[optind].equals("-d")) {
0N/A debug = true;
0N/A } else if (argv[optind].equals("--")) {
0N/A optind++;
0N/A break;
0N/A } else if (argv[optind].startsWith("-")) {
0N/A System.out.println(
0N/A"Usage: msgsend [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
0N/A System.out.println(
0N/A"\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]");
0N/A System.out.println(
0N/A"\t[-f record-mailbox] [-M transport-host] [-a attach-file] [-d] [address]");
0N/A System.exit(1);
0N/A } else {
0N/A break;
0N/A }
0N/A }
0N/A
0N/A try {
97N/A /*
97N/A * Prompt for To and Subject, if not specified.
97N/A */
0N/A if (optind < argv.length) {
0N/A // XXX - concatenate all remaining arguments
0N/A to = argv[optind];
0N/A System.out.println("To: " + to);
0N/A } else {
0N/A System.out.print("To: ");
0N/A System.out.flush();
0N/A to = in.readLine();
0N/A }
0N/A if (subject == null) {
0N/A System.out.print("Subject: ");
0N/A System.out.flush();
0N/A subject = in.readLine();
0N/A } else {
0N/A System.out.println("Subject: " + subject);
0N/A }
0N/A
97N/A /*
97N/A * Initialize the JavaMail Session.
97N/A */
0N/A Properties props = System.getProperties();
0N/A // XXX - could use Session.getTransport() and Transport.connect()
0N/A // XXX - assume we're using SMTP
0N/A if (mailhost != null)
0N/A props.put("mail.smtp.host", mailhost);
0N/A
0N/A // Get a Session object
0N/A Session session = Session.getInstance(props, null);
0N/A if (debug)
0N/A session.setDebug(true);
0N/A
97N/A /*
97N/A * Construct the message and send it.
97N/A */
0N/A Message msg = new MimeMessage(session);
0N/A if (from != null)
0N/A msg.setFrom(new InternetAddress(from));
0N/A else
0N/A msg.setFrom();
0N/A
0N/A msg.setRecipients(Message.RecipientType.TO,
0N/A InternetAddress.parse(to, false));
0N/A if (cc != null)
0N/A msg.setRecipients(Message.RecipientType.CC,
0N/A InternetAddress.parse(cc, false));
0N/A if (bcc != null)
0N/A msg.setRecipients(Message.RecipientType.BCC,
0N/A InternetAddress.parse(bcc, false));
0N/A
0N/A msg.setSubject(subject);
0N/A
0N/A String text = collect(in);
0N/A
0N/A if (file != null) {
0N/A // Attach the specified file.
0N/A // We need a multipart message to hold the attachment.
0N/A MimeBodyPart mbp1 = new MimeBodyPart();
0N/A mbp1.setText(text);
0N/A MimeBodyPart mbp2 = new MimeBodyPart();
0N/A mbp2.attachFile(file);
0N/A MimeMultipart mp = new MimeMultipart();
0N/A mp.addBodyPart(mbp1);
0N/A mp.addBodyPart(mbp2);
0N/A msg.setContent(mp);
0N/A } else {
0N/A // If the desired charset is known, you can use
0N/A // setText(text, charset)
0N/A msg.setText(text);
0N/A }
0N/A
0N/A msg.setHeader("X-Mailer", mailer);
0N/A msg.setSentDate(new Date());
0N/A
0N/A // send the thing off
0N/A Transport.send(msg);
0N/A
0N/A System.out.println("\nMail was sent successfully.");
0N/A
97N/A /*
97N/A * Save a copy of the message, if requested.
97N/A */
0N/A if (record != null) {
0N/A // Get a Store object
0N/A Store store = null;
0N/A if (url != null) {
0N/A URLName urln = new URLName(url);
0N/A store = session.getStore(urln);
0N/A store.connect();
0N/A } else {
0N/A if (protocol != null)
0N/A store = session.getStore(protocol);
0N/A else
0N/A store = session.getStore();
0N/A
0N/A // Connect
0N/A if (host != null || user != null || password != null)
0N/A store.connect(host, user, password);
0N/A else
0N/A store.connect();
0N/A }
0N/A
0N/A // Get record Folder. Create if it does not exist.
0N/A Folder folder = store.getFolder(record);
0N/A if (folder == null) {
0N/A System.err.println("Can't get record folder.");
0N/A System.exit(1);
0N/A }
0N/A if (!folder.exists())
0N/A folder.create(Folder.HOLDS_MESSAGES);
0N/A
0N/A Message[] msgs = new Message[1];
0N/A msgs[0] = msg;
0N/A folder.appendMessages(msgs);
0N/A
0N/A System.out.println("Mail was recorded successfully.");
0N/A }
0N/A
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
97N/A /**
97N/A * Read the body of the message until EOF.
97N/A */
0N/A public static String collect(BufferedReader in) throws IOException {
0N/A String line;
0N/A StringBuffer sb = new StringBuffer();
0N/A while ((line = in.readLine()) != null) {
0N/A sb.append(line);
0N/A sb.append("\n");
0N/A }
0N/A return sb.toString();
0N/A }
0N/A}