Debian Codegraph @

demo/src/main/java/sendhtml.java

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