Debian Codegraph @

demo/src/main/java/msgsend.java

raw
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.util.Properties;
13 import java.util.Date;
14
15 import jakarta.mail.*;
16 import jakarta.mail.internet.*;
17
18 /**
19 * Demo app that shows how to construct and send an RFC822
20 * (singlepart) message.
21 *
22 * XXX - allow more than one recipient on the command line
23 *
24 * @author Max Spivak
25 * @author Bill Shannon
26 */
27
28 public class msgsend {
29
30 public static void main(String[] argv) {
31 String to, subject = null, from = null,
32 cc = null, bcc = null, url = null;
33 String mailhost = null;
34 String mailer = "msgsend";
35 String file = null;
36 String protocol = null, host = null, user = null, password = null;
37 String record = null; // name of folder in which to record mail
38 boolean debug = false;
39 BufferedReader in =
40 new BufferedReader(new InputStreamReader(System.in));
41 int optind;
42
43 /*
44 * Process command line arguments.
45 */
46 for (optind = 0; optind < argv.length; optind++) {
47 if (argv[optind].equals("-T")) {
48 protocol = argv[++optind];
49 } else if (argv[optind].equals("-H")) {
50 host = argv[++optind];
51 } else if (argv[optind].equals("-U")) {
52 user = argv[++optind];
53 } else if (argv[optind].equals("-P")) {
54 password = argv[++optind];
55 } else if (argv[optind].equals("-M")) {
56 mailhost = argv[++optind];
57 } else if (argv[optind].equals("-f")) {
58 record = argv[++optind];
59 } else if (argv[optind].equals("-a")) {
60 file = argv[++optind];
61 } else if (argv[optind].equals("-s")) {
62 subject = argv[++optind];
63 } else if (argv[optind].equals("-o")) { // originator
64 from = argv[++optind];
65 } else if (argv[optind].equals("-c")) {
66 cc = argv[++optind];
67 } else if (argv[optind].equals("-b")) {
68 bcc = argv[++optind];
69 } else if (argv[optind].equals("-L")) {
70 url = argv[++optind];
71 } else if (argv[optind].equals("-d")) {
72 debug = true;
73 } else if (argv[optind].equals("--")) {
74 optind++;
75 break;
76 } else if (argv[optind].startsWith("-")) {
77 System.out.println(
78 "Usage: msgsend [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
79 System.out.println(
80 "\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]");
81 System.out.println(
82 "\t[-f record-mailbox] [-M transport-host] [-a attach-file] [-d] [address]");
83 System.exit(1);
84 } else {
85 break;
86 }
87 }
88
89 try {
90 /*
91 * Prompt for To and Subject, if not specified.
92 */
93 if (optind < argv.length) {
94 // XXX - concatenate all remaining arguments
95 to = argv[optind];
96 System.out.println("To: " + to);
97 } else {
98 System.out.print("To: ");
99 System.out.flush();
100 to = in.readLine();
101 }
102 if (subject == null) {
103 System.out.print("Subject: ");
104 System.out.flush();
105 subject = in.readLine();
106 } else {
107 System.out.println("Subject: " + subject);
108 }
109
110 /*
111 * Initialize the Jakarta Mail Session.
112 */
113 Properties props = System.getProperties();
114 // XXX - could use Session.getTransport() and Transport.connect()
115 // XXX - assume we're using SMTP
116 if (mailhost != null)
117 props.put("mail.smtp.host", mailhost);
118
119 // Get a Session object
120 Session session = Session.getInstance(props, null);
121 if (debug)
122 session.setDebug(true);
123
124 /*
125 * Construct the message and send it.
126 */
127 Message msg = new MimeMessage(session);
128 if (from != null)
129 msg.setFrom(new InternetAddress(from));
130 else
131 msg.setFrom();
132
133 msg.setRecipients(Message.RecipientType.TO,
134 InternetAddress.parse(to, false));
135 if (cc != null)
136 msg.setRecipients(Message.RecipientType.CC,
137 InternetAddress.parse(cc, false));
138 if (bcc != null)
139 msg.setRecipients(Message.RecipientType.BCC,
140 InternetAddress.parse(bcc, false));
141
142 msg.setSubject(subject);
143
144 String text = collect(in);
145
146 if (file != null) {
147 // Attach the specified file.
148 // We need a multipart message to hold the attachment.
149 MimeBodyPart mbp1 = new MimeBodyPart();
150 mbp1.setText(text);
151 MimeBodyPart mbp2 = new MimeBodyPart();
152 mbp2.attachFile(file);
153 MimeMultipart mp = new MimeMultipart();
154 mp.addBodyPart(mbp1);
155 mp.addBodyPart(mbp2);
156 msg.setContent(mp);
157 } else {
158 // If the desired charset is known, you can use
159 // setText(text, charset)
160 msg.setText(text);
161 }
162
163 msg.setHeader("X-Mailer", mailer);
164 msg.setSentDate(new Date());
165
166 // send the thing off
167 Transport.send(msg);
168
169 System.out.println("\nMail was sent successfully.");
170
171 /*
172 * Save a copy of the message, if requested.
173 */
174 if (record != null) {
175 // Get a Store object
176 Store store = null;
177 if (url != null) {
178 URLName urln = new URLName(url);
179 store = session.getStore(urln);
180 store.connect();
181 } else {
182 if (protocol != null)
183 store = session.getStore(protocol);
184 else
185 store = session.getStore();
186
187 // Connect
188 if (host != null || user != null || password != null)
189 store.connect(host, user, password);
190 else
191 store.connect();
192 }
193
194 // Get record Folder. Create if it does not exist.
195 Folder folder = store.getFolder(record);
196 if (folder == null) {
197 System.err.println("Can't get record folder.");
198 System.exit(1);
199 }
200 if (!folder.exists())
201 folder.create(Folder.HOLDS_MESSAGES);
202
203 Message[] msgs = new Message[1];
204 msgs[0] = msg;
205 folder.appendMessages(msgs);
206
207 System.out.println("Mail was recorded successfully.");
208 }
209
210 } catch (Exception e) {
211 e.printStackTrace();
212 }
213 }
214
215 /**
216 * Read the body of the message until EOF.
217 */
218 public static String collect(BufferedReader in) throws IOException {
219 String line;
220 StringBuffer sb = new StringBuffer();
221 while ((line = in.readLine()) != null) {
222 sb.append(line);
223 sb.append("\n");
224 }
225 return sb.toString();
226 }
227 }