| 1 |
/* |
| 2 |
* Copyright (c) 1996, 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 java.io.*; |
| 13 |
import jakarta.mail.*; |
| 14 |
import jakarta.mail.internet.*; |
| 15 |
import jakarta.activation.*; |
| 16 |
|
| 17 |
/** |
| 18 |
* msgsendsample creates a very simple text/plain message and sends it. |
| 19 |
* <p> |
| 20 |
* usage: <code>java msgsendsample <i>to from smtphost true|false</i></code> |
| 21 |
* where <i>to</i> and <i>from</i> are the destination and |
| 22 |
* origin email addresses, respectively, and <i>smtphost</i> |
| 23 |
* is the hostname of the machine that has the smtp server |
| 24 |
* running. The last parameter either turns on or turns off |
| 25 |
* debugging during sending. |
| 26 |
* |
| 27 |
* @author Max Spivak |
| 28 |
*/ |
| 29 |
public class msgsendsample { |
| 30 |
static String msgText = "This is a message body.\nHere's the second line."; |
| 31 |
|
| 32 |
public static void main(String[] args) { |
| 33 |
if (args.length != 4) { |
| 34 |
usage(); |
| 35 |
System.exit(1); |
| 36 |
} |
| 37 |
|
| 38 |
System.out.println(); |
| 39 |
|
| 40 |
String to = args[0]; |
| 41 |
String from = args[1]; |
| 42 |
String host = args[2]; |
| 43 |
boolean debug = Boolean.valueOf(args[3]).booleanValue(); |
| 44 |
|
| 45 |
// create some properties and get the default Session |
| 46 |
Properties props = new Properties(); |
| 47 |
props.put("mail.smtp.host", host); |
| 48 |
if (debug) props.put("mail.debug", args[3]); |
| 49 |
|
| 50 |
Session session = Session.getInstance(props, null); |
| 51 |
session.setDebug(debug); |
| 52 |
|
| 53 |
try { |
| 54 |
// create a message |
| 55 |
MimeMessage msg = new MimeMessage(session); |
| 56 |
msg.setFrom(new InternetAddress(from)); |
| 57 |
InternetAddress[] address = {new InternetAddress(to)}; |
| 58 |
msg.setRecipients(Message.RecipientType.TO, address); |
| 59 |
msg.setSubject("Jakarta Mail APIs Test"); |
| 60 |
msg.setSentDate(new Date()); |
| 61 |
// If the desired charset is known, you can use |
| 62 |
// setText(text, charset) |
| 63 |
msg.setText(msgText); |
| 64 |
|
| 65 |
Transport.send(msg); |
| 66 |
} catch (MessagingException mex) { |
| 67 |
System.out.println("\n--Exception handling in msgsendsample.java"); |
| 68 |
|
| 69 |
mex.printStackTrace(); |
| 70 |
System.out.println(); |
| 71 |
Exception ex = mex; |
| 72 |
do { |
| 73 |
if (ex instanceof SendFailedException) { |
| 74 |
SendFailedException sfex = (SendFailedException)ex; |
| 75 |
Address[] invalid = sfex.getInvalidAddresses(); |
| 76 |
if (invalid != null) { |
| 77 |
System.out.println(" ** Invalid Addresses"); |
| 78 |
for (int i = 0; i < invalid.length; i++) |
| 79 |
System.out.println(" " + invalid[i]); |
| 80 |
} |
| 81 |
Address[] validUnsent = sfex.getValidUnsentAddresses(); |
| 82 |
if (validUnsent != null) { |
| 83 |
System.out.println(" ** ValidUnsent Addresses"); |
| 84 |
for (int i = 0; i < validUnsent.length; i++) |
| 85 |
System.out.println(" "+validUnsent[i]); |
| 86 |
} |
| 87 |
Address[] validSent = sfex.getValidSentAddresses(); |
| 88 |
if (validSent != null) { |
| 89 |
System.out.println(" ** ValidSent Addresses"); |
| 90 |
for (int i = 0; i < validSent.length; i++) |
| 91 |
System.out.println(" "+validSent[i]); |
| 92 |
} |
| 93 |
} |
| 94 |
System.out.println(); |
| 95 |
if (ex instanceof MessagingException) |
| 96 |
ex = ((MessagingException)ex).getNextException(); |
| 97 |
else |
| 98 |
ex = null; |
| 99 |
} while (ex != null); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
private static void usage() { |
| 104 |
System.out.println("usage: java msgsendsample <to> <from> <smtp> true|false"); |
| 105 |
} |
| 106 |
} |