| 1 |
/* |
| 2 |
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved. |
| 3 |
* |
| 4 |
* This program and the accompanying materials are made available under the |
| 5 |
* terms of the Eclipse Distribution License v. 1.0, which is available at |
| 6 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 7 |
* |
| 8 |
* SPDX-License-Identifier: BSD-3-Clause |
| 9 |
*/ |
| 10 |
|
| 11 |
import java.util.*; |
| 12 |
import jakarta.mail.*; |
| 13 |
import jakarta.mail.internet.*; |
| 14 |
import jakarta.mail.event.*; |
| 15 |
import jakarta.activation.*; |
| 16 |
|
| 17 |
/** |
| 18 |
* transport is a simple program that creates a message, explicitly |
| 19 |
* retrieves a Transport from the session based on the type of the |
| 20 |
* address (it's InternetAddress, so SMTP will be used) and sends |
| 21 |
* the message. |
| 22 |
* |
| 23 |
* usage: <code>java transport <i>"toaddr1[, toaddr2]*" from smtphost |
| 24 |
* true|false</i></code><br> |
| 25 |
* where <i>to</i> and <i>from</i> are the destination and |
| 26 |
* origin email addresses, respectively, and <i>smtphost</i> |
| 27 |
* is the hostname of the machine that has the smtp server |
| 28 |
* running. The <i>to</i> addresses can be either a single email |
| 29 |
* address or a comma-separated list of email addresses in |
| 30 |
* quotes, i.e. "joe@machine, jane, [email protected]" |
| 31 |
* The last parameter either turns on or turns off |
| 32 |
* debugging during sending. |
| 33 |
* |
| 34 |
* @author Max Spivak |
| 35 |
*/ |
| 36 |
public class transport implements ConnectionListener, TransportListener { |
| 37 |
static String msgText = "This is a message body.\nHere's the second line."; |
| 38 |
static String msgText2 = "\nThis was sent by transport.java demo program."; |
| 39 |
|
| 40 |
public static void main(String[] args) { |
| 41 |
Properties props = System.getProperties(); |
| 42 |
// parse the arguments |
| 43 |
InternetAddress[] addrs = null; |
| 44 |
InternetAddress from; |
| 45 |
boolean debug = false; |
| 46 |
if (args.length != 4) { |
| 47 |
usage(); |
| 48 |
return; |
| 49 |
} else { |
| 50 |
props.put("mail.smtp.host", args[2]); |
| 51 |
if (args[3].equals("true")) { |
| 52 |
debug = true; |
| 53 |
} else if (args[3].equals("false")) { |
| 54 |
debug = false; |
| 55 |
} else { |
| 56 |
usage(); |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
// parse the destination addresses |
| 61 |
try { |
| 62 |
addrs = InternetAddress.parse(args[0], false); |
| 63 |
from = new InternetAddress(args[1]); |
| 64 |
} catch (AddressException aex) { |
| 65 |
System.out.println("Invalid Address"); |
| 66 |
aex.printStackTrace(); |
| 67 |
return; |
| 68 |
} |
| 69 |
} |
| 70 |
// create some properties and get a Session |
| 71 |
Session session = Session.getInstance(props, null); |
| 72 |
session.setDebug(debug); |
| 73 |
|
| 74 |
transport t = new transport(); |
| 75 |
t.go(session, addrs, from); |
| 76 |
} |
| 77 |
|
| 78 |
public transport() {} |
| 79 |
|
| 80 |
public void go(Session session, InternetAddress[] toAddr, |
| 81 |
InternetAddress from) { |
| 82 |
Transport trans = null; |
| 83 |
|
| 84 |
try { |
| 85 |
// create a message |
| 86 |
Message msg = new MimeMessage(session); |
| 87 |
msg.setFrom(from); |
| 88 |
msg.setRecipients(Message.RecipientType.TO, toAddr); |
| 89 |
msg.setSubject("Jakarta Mail APIs transport.java Test"); |
| 90 |
msg.setSentDate(new Date()); // Date: header |
| 91 |
msg.setContent(msgText+msgText2, "text/plain"); |
| 92 |
msg.saveChanges(); |
| 93 |
|
| 94 |
// get the smtp transport for the address |
| 95 |
trans = session.getTransport(toAddr[0]); |
| 96 |
|
| 97 |
// register ourselves as listener for ConnectionEvents |
| 98 |
// and TransportEvents |
| 99 |
trans.addConnectionListener(this); |
| 100 |
trans.addTransportListener(this); |
| 101 |
|
| 102 |
// connect the transport |
| 103 |
trans.connect(); |
| 104 |
|
| 105 |
// send the message |
| 106 |
trans.sendMessage(msg, toAddr); |
| 107 |
|
| 108 |
// give the EventQueue enough time to fire its events |
| 109 |
try {Thread.sleep(5);}catch(InterruptedException e) {} |
| 110 |
|
| 111 |
} catch (MessagingException mex) { |
| 112 |
// give the EventQueue enough time to fire its events |
| 113 |
try {Thread.sleep(5);}catch(InterruptedException e) {} |
| 114 |
|
| 115 |
System.out.println("Sending failed with exception:"); |
| 116 |
mex.printStackTrace(); |
| 117 |
System.out.println(); |
| 118 |
Exception ex = mex; |
| 119 |
do { |
| 120 |
if (ex instanceof SendFailedException) { |
| 121 |
SendFailedException sfex = (SendFailedException)ex; |
| 122 |
Address[] invalid = sfex.getInvalidAddresses(); |
| 123 |
if (invalid != null) { |
| 124 |
System.out.println(" ** Invalid Addresses"); |
| 125 |
for (int i = 0; i < invalid.length; i++) |
| 126 |
System.out.println(" " + invalid[i]); |
| 127 |
} |
| 128 |
Address[] validUnsent = sfex.getValidUnsentAddresses(); |
| 129 |
if (validUnsent != null) { |
| 130 |
System.out.println(" ** ValidUnsent Addresses"); |
| 131 |
for (int i = 0; i < validUnsent.length; i++) |
| 132 |
System.out.println(" "+validUnsent[i]); |
| 133 |
} |
| 134 |
Address[] validSent = sfex.getValidSentAddresses(); |
| 135 |
if (validSent != null) { |
| 136 |
System.out.println(" ** ValidSent Addresses"); |
| 137 |
for (int i = 0; i < validSent.length; i++) |
| 138 |
System.out.println(" "+validSent[i]); |
| 139 |
} |
| 140 |
} |
| 141 |
System.out.println(); |
| 142 |
if (ex instanceof MessagingException) |
| 143 |
ex = ((MessagingException)ex).getNextException(); |
| 144 |
else |
| 145 |
ex = null; |
| 146 |
} while (ex != null); |
| 147 |
} finally { |
| 148 |
try { |
| 149 |
// close the transport |
| 150 |
if (trans != null) |
| 151 |
trans.close(); |
| 152 |
} catch (MessagingException mex) { /* ignore */ } |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// implement ConnectionListener interface |
| 157 |
public void opened(ConnectionEvent e) { |
| 158 |
System.out.println(">>> ConnectionListener.opened()"); |
| 159 |
} |
| 160 |
public void disconnected(ConnectionEvent e) {} |
| 161 |
public void closed(ConnectionEvent e) { |
| 162 |
System.out.println(">>> ConnectionListener.closed()"); |
| 163 |
} |
| 164 |
|
| 165 |
// implement TransportListener interface |
| 166 |
public void messageDelivered(TransportEvent e) { |
| 167 |
System.out.println(">>> TransportListener.messageDelivered()."); |
| 168 |
System.out.println(" Valid Addresses:"); |
| 169 |
Address[] valid = e.getValidSentAddresses(); |
| 170 |
if (valid != null) { |
| 171 |
for (int i = 0; i < valid.length; i++) |
| 172 |
System.out.println(" " + valid[i]); |
| 173 |
} |
| 174 |
} |
| 175 |
public void messageNotDelivered(TransportEvent e) { |
| 176 |
System.out.println(">>> TransportListener.messageNotDelivered()."); |
| 177 |
System.out.println(" Invalid Addresses:"); |
| 178 |
Address[] invalid = e.getInvalidAddresses(); |
| 179 |
if (invalid != null) { |
| 180 |
for (int i = 0; i < invalid.length; i++) |
| 181 |
System.out.println(" " + invalid[i]); |
| 182 |
} |
| 183 |
} |
| 184 |
public void messagePartiallyDelivered(TransportEvent e) { |
| 185 |
System.out.println(">>> TransportListener.messagePartiallyDelivered()."); |
| 186 |
System.out.println(" Valid Addresses:"); |
| 187 |
Address[] valid = e.getValidSentAddresses(); |
| 188 |
if (valid != null) { |
| 189 |
for (int i = 0; i < valid.length; i++) |
| 190 |
System.out.println(" " + valid[i]); |
| 191 |
} |
| 192 |
System.out.println(" Valid Unsent Addresses:"); |
| 193 |
Address[] unsent = e.getValidUnsentAddresses(); |
| 194 |
if (unsent != null) { |
| 195 |
for (int i = 0; i < unsent.length; i++) |
| 196 |
System.out.println(" " + unsent[i]); |
| 197 |
} |
| 198 |
System.out.println(" Invalid Addresses:"); |
| 199 |
Address[] invalid = e.getInvalidAddresses(); |
| 200 |
if (invalid != null) { |
| 201 |
for (int i = 0; i < invalid.length; i++) |
| 202 |
System.out.println(" " + invalid[i]); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
private static void usage() { |
| 207 |
System.out.println( |
| 208 |
"usage: java transport \"<to1>[, <to2>]*\" <from> <smtp> true|false"); |
| 209 |
System.out.println( |
| 210 |
"example: java transport \"joe@machine, jane\" senderaddr smtphost false"); |
| 211 |
} |
| 212 |
} |