| 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.io.*; |
| 12 |
import java.net.InetAddress; |
| 13 |
import java.util.Properties; |
| 14 |
import java.util.Date; |
| 15 |
|
| 16 |
import jakarta.mail.*; |
| 17 |
import jakarta.mail.internet.*; |
| 18 |
|
| 19 |
import com.sun.mail.smtp.*; |
| 20 |
|
| 21 |
/** |
| 22 |
* Demo app that shows how to construct and send an RFC822 |
| 23 |
* (singlepart) message. |
| 24 |
* |
| 25 |
* XXX - allow more than one recipient on the command line |
| 26 |
* |
| 27 |
* This is just a variant of msgsend.java that demonstrates use of |
| 28 |
* some SMTP-specific features. |
| 29 |
* |
| 30 |
* @author Max Spivak |
| 31 |
* @author Bill Shannon |
| 32 |
*/ |
| 33 |
|
| 34 |
public class smtpsend { |
| 35 |
|
| 36 |
/** |
| 37 |
* Example of how to extend the SMTPTransport class. |
| 38 |
* This example illustrates how to issue the XACT |
| 39 |
* command before the SMTPTransport issues the DATA |
| 40 |
* command. |
| 41 |
* |
| 42 |
public static class SMTPExtension extends SMTPTransport { |
| 43 |
public SMTPExtension(Session session, URLName url) { |
| 44 |
super(session, url); |
| 45 |
// to check that we're being used |
| 46 |
System.out.println("SMTPExtension: constructed"); |
| 47 |
} |
| 48 |
|
| 49 |
protected synchronized OutputStream data() throws MessagingException { |
| 50 |
if (supportsExtension("XACCOUNTING")) |
| 51 |
issueCommand("XACT", 250); |
| 52 |
return super.data(); |
| 53 |
} |
| 54 |
} |
| 55 |
*/ |
| 56 |
|
| 57 |
public static void main(String[] argv) { |
| 58 |
String to, subject = null, from = null, |
| 59 |
cc = null, bcc = null, url = null; |
| 60 |
String mailhost = null; |
| 61 |
String mailer = "smtpsend"; |
| 62 |
String file = null; |
| 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 |
boolean verbose = false; |
| 67 |
boolean auth = false; |
| 68 |
String prot = "smtp"; |
| 69 |
BufferedReader in = |
| 70 |
new BufferedReader(new InputStreamReader(System.in)); |
| 71 |
int optind; |
| 72 |
|
| 73 |
/* |
| 74 |
* Process command line arguments. |
| 75 |
*/ |
| 76 |
for (optind = 0; optind < argv.length; optind++) { |
| 77 |
if (argv[optind].equals("-T")) { |
| 78 |
protocol = argv[++optind]; |
| 79 |
} else if (argv[optind].equals("-H")) { |
| 80 |
host = argv[++optind]; |
| 81 |
} else if (argv[optind].equals("-U")) { |
| 82 |
user = argv[++optind]; |
| 83 |
} else if (argv[optind].equals("-P")) { |
| 84 |
password = argv[++optind]; |
| 85 |
} else if (argv[optind].equals("-M")) { |
| 86 |
mailhost = argv[++optind]; |
| 87 |
} else if (argv[optind].equals("-f")) { |
| 88 |
record = argv[++optind]; |
| 89 |
} else if (argv[optind].equals("-a")) { |
| 90 |
file = argv[++optind]; |
| 91 |
} else if (argv[optind].equals("-s")) { |
| 92 |
subject = argv[++optind]; |
| 93 |
} else if (argv[optind].equals("-o")) { // originator |
| 94 |
from = argv[++optind]; |
| 95 |
} else if (argv[optind].equals("-c")) { |
| 96 |
cc = argv[++optind]; |
| 97 |
} else if (argv[optind].equals("-b")) { |
| 98 |
bcc = argv[++optind]; |
| 99 |
} else if (argv[optind].equals("-L")) { |
| 100 |
url = argv[++optind]; |
| 101 |
} else if (argv[optind].equals("-d")) { |
| 102 |
debug = true; |
| 103 |
} else if (argv[optind].equals("-v")) { |
| 104 |
verbose = true; |
| 105 |
} else if (argv[optind].equals("-A")) { |
| 106 |
auth = true; |
| 107 |
} else if (argv[optind].equals("-S")) { |
| 108 |
prot = "smtps"; |
| 109 |
} else if (argv[optind].equals("--")) { |
| 110 |
optind++; |
| 111 |
break; |
| 112 |
} else if (argv[optind].startsWith("-")) { |
| 113 |
System.out.println( |
| 114 |
"Usage: smtpsend [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]"); |
| 115 |
System.out.println( |
| 116 |
"\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]"); |
| 117 |
System.out.println( |
| 118 |
"\t[-f record-mailbox] [-M transport-host] [-d] [-a attach-file]"); |
| 119 |
System.out.println( |
| 120 |
"\t[-v] [-A] [-S] [address]"); |
| 121 |
System.exit(1); |
| 122 |
} else { |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
try { |
| 128 |
/* |
| 129 |
* Prompt for To and Subject, if not specified. |
| 130 |
*/ |
| 131 |
if (optind < argv.length) { |
| 132 |
// XXX - concatenate all remaining arguments |
| 133 |
to = argv[optind]; |
| 134 |
System.out.println("To: " + to); |
| 135 |
} else { |
| 136 |
System.out.print("To: "); |
| 137 |
System.out.flush(); |
| 138 |
to = in.readLine(); |
| 139 |
} |
| 140 |
if (subject == null) { |
| 141 |
System.out.print("Subject: "); |
| 142 |
System.out.flush(); |
| 143 |
subject = in.readLine(); |
| 144 |
} else { |
| 145 |
System.out.println("Subject: " + subject); |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* Initialize the Jakarta Mail Session. |
| 150 |
*/ |
| 151 |
Properties props = System.getProperties(); |
| 152 |
if (mailhost != null) |
| 153 |
props.put("mail." + prot + ".host", mailhost); |
| 154 |
if (auth) |
| 155 |
props.put("mail." + prot + ".auth", "true"); |
| 156 |
|
| 157 |
/* |
| 158 |
* Create a Provider representing our extended SMTP transport |
| 159 |
* and set the property to use our provider. |
| 160 |
* |
| 161 |
Provider p = new Provider(Provider.Type.TRANSPORT, prot, |
| 162 |
"smtpsend$SMTPExtension", "Jakarta Mail demo", "no version"); |
| 163 |
props.put("mail." + prot + ".class", "smtpsend$SMTPExtension"); |
| 164 |
*/ |
| 165 |
|
| 166 |
// Get a Session object |
| 167 |
Session session = Session.getInstance(props, null); |
| 168 |
if (debug) |
| 169 |
session.setDebug(true); |
| 170 |
|
| 171 |
/* |
| 172 |
* Register our extended SMTP transport. |
| 173 |
* |
| 174 |
session.addProvider(p); |
| 175 |
*/ |
| 176 |
|
| 177 |
/* |
| 178 |
* Construct the message and send it. |
| 179 |
*/ |
| 180 |
Message msg = new MimeMessage(session); |
| 181 |
if (from != null) |
| 182 |
msg.setFrom(new InternetAddress(from)); |
| 183 |
else |
| 184 |
msg.setFrom(); |
| 185 |
|
| 186 |
msg.setRecipients(Message.RecipientType.TO, |
| 187 |
InternetAddress.parse(to, false)); |
| 188 |
if (cc != null) |
| 189 |
msg.setRecipients(Message.RecipientType.CC, |
| 190 |
InternetAddress.parse(cc, false)); |
| 191 |
if (bcc != null) |
| 192 |
msg.setRecipients(Message.RecipientType.BCC, |
| 193 |
InternetAddress.parse(bcc, false)); |
| 194 |
|
| 195 |
msg.setSubject(subject); |
| 196 |
|
| 197 |
String text = collect(in); |
| 198 |
|
| 199 |
if (file != null) { |
| 200 |
// Attach the specified file. |
| 201 |
// We need a multipart message to hold the attachment. |
| 202 |
MimeBodyPart mbp1 = new MimeBodyPart(); |
| 203 |
mbp1.setText(text); |
| 204 |
MimeBodyPart mbp2 = new MimeBodyPart(); |
| 205 |
mbp2.attachFile(file); |
| 206 |
MimeMultipart mp = new MimeMultipart(); |
| 207 |
mp.addBodyPart(mbp1); |
| 208 |
mp.addBodyPart(mbp2); |
| 209 |
msg.setContent(mp); |
| 210 |
} else { |
| 211 |
// If the desired charset is known, you can use |
| 212 |
// setText(text, charset) |
| 213 |
msg.setText(text); |
| 214 |
} |
| 215 |
|
| 216 |
msg.setHeader("X-Mailer", mailer); |
| 217 |
msg.setSentDate(new Date()); |
| 218 |
|
| 219 |
// send the thing off |
| 220 |
/* |
| 221 |
* The simple way to send a message is this: |
| 222 |
* |
| 223 |
Transport.send(msg); |
| 224 |
* |
| 225 |
* But we're going to use some SMTP-specific features for |
| 226 |
* demonstration purposes so we need to manage the Transport |
| 227 |
* object explicitly. |
| 228 |
*/ |
| 229 |
SMTPTransport t = |
| 230 |
(SMTPTransport)session.getTransport(prot); |
| 231 |
try { |
| 232 |
if (auth) |
| 233 |
t.connect(mailhost, user, password); |
| 234 |
else |
| 235 |
t.connect(); |
| 236 |
t.sendMessage(msg, msg.getAllRecipients()); |
| 237 |
} finally { |
| 238 |
if (verbose) |
| 239 |
System.out.println("Response: " + |
| 240 |
t.getLastServerResponse()); |
| 241 |
t.close(); |
| 242 |
} |
| 243 |
|
| 244 |
System.out.println("\nMail was sent successfully."); |
| 245 |
|
| 246 |
/* |
| 247 |
* Save a copy of the message, if requested. |
| 248 |
*/ |
| 249 |
if (record != null) { |
| 250 |
// Get a Store object |
| 251 |
Store store = null; |
| 252 |
if (url != null) { |
| 253 |
URLName urln = new URLName(url); |
| 254 |
store = session.getStore(urln); |
| 255 |
store.connect(); |
| 256 |
} else { |
| 257 |
if (protocol != null) |
| 258 |
store = session.getStore(protocol); |
| 259 |
else |
| 260 |
store = session.getStore(); |
| 261 |
|
| 262 |
// Connect |
| 263 |
if (host != null || user != null || password != null) |
| 264 |
store.connect(host, user, password); |
| 265 |
else |
| 266 |
store.connect(); |
| 267 |
} |
| 268 |
|
| 269 |
// Get record Folder. Create if it does not exist. |
| 270 |
Folder folder = store.getFolder(record); |
| 271 |
if (folder == null) { |
| 272 |
System.err.println("Can't get record folder."); |
| 273 |
System.exit(1); |
| 274 |
} |
| 275 |
if (!folder.exists()) |
| 276 |
folder.create(Folder.HOLDS_MESSAGES); |
| 277 |
|
| 278 |
Message[] msgs = new Message[1]; |
| 279 |
msgs[0] = msg; |
| 280 |
folder.appendMessages(msgs); |
| 281 |
|
| 282 |
System.out.println("Mail was recorded successfully."); |
| 283 |
} |
| 284 |
|
| 285 |
} catch (Exception e) { |
| 286 |
/* |
| 287 |
* Handle SMTP-specific exceptions. |
| 288 |
*/ |
| 289 |
if (e instanceof SendFailedException) { |
| 290 |
MessagingException sfe = (MessagingException)e; |
| 291 |
if (sfe instanceof SMTPSendFailedException) { |
| 292 |
SMTPSendFailedException ssfe = |
| 293 |
(SMTPSendFailedException)sfe; |
| 294 |
System.out.println("SMTP SEND FAILED:"); |
| 295 |
if (verbose) |
| 296 |
System.out.println(ssfe.toString()); |
| 297 |
System.out.println(" Command: " + ssfe.getCommand()); |
| 298 |
System.out.println(" RetCode: " + ssfe.getReturnCode()); |
| 299 |
System.out.println(" Response: " + ssfe.getMessage()); |
| 300 |
} else { |
| 301 |
if (verbose) |
| 302 |
System.out.println("Send failed: " + sfe.toString()); |
| 303 |
} |
| 304 |
Exception ne; |
| 305 |
while ((ne = sfe.getNextException()) != null && |
| 306 |
ne instanceof MessagingException) { |
| 307 |
sfe = (MessagingException)ne; |
| 308 |
if (sfe instanceof SMTPAddressFailedException) { |
| 309 |
SMTPAddressFailedException ssfe = |
| 310 |
(SMTPAddressFailedException)sfe; |
| 311 |
System.out.println("ADDRESS FAILED:"); |
| 312 |
if (verbose) |
| 313 |
System.out.println(ssfe.toString()); |
| 314 |
System.out.println(" Address: " + ssfe.getAddress()); |
| 315 |
System.out.println(" Command: " + ssfe.getCommand()); |
| 316 |
System.out.println(" RetCode: " + ssfe.getReturnCode()); |
| 317 |
System.out.println(" Response: " + ssfe.getMessage()); |
| 318 |
} else if (sfe instanceof SMTPAddressSucceededException) { |
| 319 |
System.out.println("ADDRESS SUCCEEDED:"); |
| 320 |
SMTPAddressSucceededException ssfe = |
| 321 |
(SMTPAddressSucceededException)sfe; |
| 322 |
if (verbose) |
| 323 |
System.out.println(ssfe.toString()); |
| 324 |
System.out.println(" Address: " + ssfe.getAddress()); |
| 325 |
System.out.println(" Command: " + ssfe.getCommand()); |
| 326 |
System.out.println(" RetCode: " + ssfe.getReturnCode()); |
| 327 |
System.out.println(" Response: " + ssfe.getMessage()); |
| 328 |
} |
| 329 |
} |
| 330 |
} else { |
| 331 |
System.out.println("Got Exception: " + e); |
| 332 |
if (verbose) |
| 333 |
e.printStackTrace(); |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Read the body of the message until EOF. |
| 340 |
*/ |
| 341 |
public static String collect(BufferedReader in) throws IOException { |
| 342 |
String line; |
| 343 |
StringBuffer sb = new StringBuffer(); |
| 344 |
while ((line = in.readLine()) != null) { |
| 345 |
sb.append(line); |
| 346 |
sb.append("\n"); |
| 347 |
} |
| 348 |
return sb.toString(); |
| 349 |
} |
| 350 |
} |