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