0N/A/*
364N/A * Copyright (c) 1997-2011 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.util.*;
0N/Aimport javax.mail.*;
0N/Aimport javax.mail.internet.*;
0N/Aimport javax.mail.event.*;
0N/Aimport javax.activation.*;
0N/A
0N/A/**
0N/A * transport is a simple program that creates a message, explicitly
0N/A * retrieves a Transport from the session based on the type of the
0N/A * address (it's InternetAddress, so SMTP will be used) and sends
0N/A * the message.
0N/A *
0N/A * usage: <code>java transport <i>"toaddr1[, toaddr2]*" from smtphost
0N/A * true|false</i></code><br>
0N/A * where <i>to</i> and <i>from</i> are the destination and
0N/A * origin email addresses, respectively, and <i>smtphost</i>
0N/A * is the hostname of the machine that has the smtp server
0N/A * running. The <i>to</i> addresses can be either a single email
0N/A * address or a comma-separated list of email addresses in
0N/A * quotes, i.e. "joe@machine, jane, max@server.com"
0N/A * The last parameter either turns on or turns off
0N/A * debugging during sending.
0N/A *
0N/A * @author Max Spivak
0N/A */
0N/Apublic class transport implements ConnectionListener, TransportListener {
0N/A static String msgText = "This is a message body.\nHere's the second line.";
0N/A static String msgText2 = "\nThis was sent by transport.java demo program.";
0N/A
0N/A public static void main(String[] args) {
36N/A Properties props = System.getProperties();
0N/A // parse the arguments
0N/A InternetAddress[] addrs = null;
0N/A InternetAddress from;
0N/A boolean debug = false;
0N/A if (args.length != 4) {
0N/A usage();
0N/A return;
0N/A } else {
0N/A props.put("mail.smtp.host", args[2]);
0N/A if (args[3].equals("true")) {
0N/A debug = true;
0N/A } else if (args[3].equals("false")) {
0N/A debug = false;
0N/A } else {
0N/A usage();
0N/A return;
0N/A }
0N/A
0N/A // parse the destination addresses
0N/A try {
0N/A addrs = InternetAddress.parse(args[0], false);
0N/A from = new InternetAddress(args[1]);
0N/A } catch (AddressException aex) {
0N/A System.out.println("Invalid Address");
0N/A aex.printStackTrace();
0N/A return;
0N/A }
0N/A }
0N/A // create some properties and get a Session
0N/A Session session = Session.getInstance(props, null);
0N/A session.setDebug(debug);
0N/A
0N/A transport t = new transport();
0N/A t.go(session, addrs, from);
0N/A }
0N/A
0N/A public transport() {}
0N/A
0N/A public void go(Session session, InternetAddress[] toAddr,
0N/A InternetAddress from) {
0N/A Transport trans = null;
0N/A
0N/A try {
0N/A // create a message
0N/A Message msg = new MimeMessage(session);
0N/A msg.setFrom(from);
0N/A msg.setRecipients(Message.RecipientType.TO, toAddr);
0N/A msg.setSubject("JavaMail APIs transport.java Test");
0N/A msg.setSentDate(new Date()); // Date: header
0N/A msg.setContent(msgText+msgText2, "text/plain");
0N/A msg.saveChanges();
0N/A
0N/A // get the smtp transport for the address
0N/A trans = session.getTransport(toAddr[0]);
0N/A
0N/A // register ourselves as listener for ConnectionEvents
0N/A // and TransportEvents
0N/A trans.addConnectionListener(this);
0N/A trans.addTransportListener(this);
0N/A
0N/A // connect the transport
0N/A trans.connect();
0N/A
0N/A // send the message
0N/A trans.sendMessage(msg, toAddr);
0N/A
0N/A // give the EventQueue enough time to fire its events
0N/A try {Thread.sleep(5);}catch(InterruptedException e) {}
0N/A
0N/A } catch (MessagingException mex) {
0N/A // give the EventQueue enough time to fire its events
0N/A try {Thread.sleep(5);}catch(InterruptedException e) {}
0N/A
36N/A System.out.println("Sending failed with exception:");
0N/A mex.printStackTrace();
0N/A System.out.println();
0N/A Exception ex = mex;
0N/A do {
0N/A if (ex instanceof SendFailedException) {
0N/A SendFailedException sfex = (SendFailedException)ex;
0N/A Address[] invalid = sfex.getInvalidAddresses();
0N/A if (invalid != null) {
0N/A System.out.println(" ** Invalid Addresses");
364N/A for (int i = 0; i < invalid.length; i++)
364N/A System.out.println(" " + invalid[i]);
0N/A }
0N/A Address[] validUnsent = sfex.getValidUnsentAddresses();
0N/A if (validUnsent != null) {
0N/A System.out.println(" ** ValidUnsent Addresses");
364N/A for (int i = 0; i < validUnsent.length; i++)
364N/A System.out.println(" "+validUnsent[i]);
0N/A }
0N/A Address[] validSent = sfex.getValidSentAddresses();
0N/A if (validSent != null) {
0N/A System.out.println(" ** ValidSent Addresses");
364N/A for (int i = 0; i < validSent.length; i++)
364N/A System.out.println(" "+validSent[i]);
0N/A }
0N/A }
0N/A System.out.println();
0N/A if (ex instanceof MessagingException)
0N/A ex = ((MessagingException)ex).getNextException();
0N/A else
0N/A ex = null;
0N/A } while (ex != null);
0N/A } finally {
0N/A try {
0N/A // close the transport
0N/A if (trans != null)
0N/A trans.close();
0N/A } catch (MessagingException mex) { /* ignore */ }
0N/A }
0N/A }
0N/A
0N/A // implement ConnectionListener interface
0N/A public void opened(ConnectionEvent e) {
0N/A System.out.println(">>> ConnectionListener.opened()");
0N/A }
0N/A public void disconnected(ConnectionEvent e) {}
0N/A public void closed(ConnectionEvent e) {
0N/A System.out.println(">>> ConnectionListener.closed()");
0N/A }
0N/A
0N/A // implement TransportListener interface
0N/A public void messageDelivered(TransportEvent e) {
36N/A System.out.println(">>> TransportListener.messageDelivered().");
0N/A System.out.println(" Valid Addresses:");
0N/A Address[] valid = e.getValidSentAddresses();
0N/A if (valid != null) {
0N/A for (int i = 0; i < valid.length; i++)
0N/A System.out.println(" " + valid[i]);
0N/A }
0N/A }
0N/A public void messageNotDelivered(TransportEvent e) {
36N/A System.out.println(">>> TransportListener.messageNotDelivered().");
0N/A System.out.println(" Invalid Addresses:");
0N/A Address[] invalid = e.getInvalidAddresses();
0N/A if (invalid != null) {
0N/A for (int i = 0; i < invalid.length; i++)
0N/A System.out.println(" " + invalid[i]);
0N/A }
0N/A }
0N/A public void messagePartiallyDelivered(TransportEvent e) {
36N/A System.out.println(">>> TransportListener.messagePartiallyDelivered().");
36N/A System.out.println(" Valid Addresses:");
36N/A Address[] valid = e.getValidSentAddresses();
36N/A if (valid != null) {
36N/A for (int i = 0; i < valid.length; i++)
36N/A System.out.println(" " + valid[i]);
36N/A }
36N/A System.out.println(" Valid Unsent Addresses:");
36N/A Address[] unsent = e.getValidUnsentAddresses();
36N/A if (unsent != null) {
36N/A for (int i = 0; i < unsent.length; i++)
36N/A System.out.println(" " + unsent[i]);
36N/A }
36N/A System.out.println(" Invalid Addresses:");
36N/A Address[] invalid = e.getInvalidAddresses();
36N/A if (invalid != null) {
36N/A for (int i = 0; i < invalid.length; i++)
36N/A System.out.println(" " + invalid[i]);
36N/A }
0N/A }
0N/A
0N/A private static void usage() {
36N/A System.out.println(
36N/A "usage: java transport \"<to1>[, <to2>]*\" <from> <smtp> true|false");
36N/A System.out.println(
36N/A "example: java transport \"joe@machine, jane\" senderaddr smtphost false");
0N/A }
0N/A}