KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sendhtml


1 /*
2  * @(#)sendhtml.java 1.9 03/04/22
3  *
4  * Copyright 1998-2003 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import java.io.*;
40 import java.util.Properties JavaDoc;
41 import java.util.Date JavaDoc;
42
43 import javax.mail.*;
44 import javax.activation.*;
45 import javax.mail.internet.*;
46
47 /**
48  * Demo app that shows how to construct and send a single part html
49  * message. Note that the same basic technique can be used to send
50  * data of any type.
51  *
52  * @author John Mani
53  * @author Bill Shannon
54  * @author Max Spivak
55  */

56
57 public class sendhtml {
58
59     public static void main(String JavaDoc[] argv) {
60     new sendhtml(argv);
61     }
62
63     public sendhtml(String JavaDoc[] argv) {
64
65     String JavaDoc to, subject = null, from = null,
66         cc = null, bcc = null, url = null;
67     String JavaDoc mailhost = null;
68     String JavaDoc mailer = "sendhtml";
69     String JavaDoc protocol = null, host = null, user = null, password = null;
70     String JavaDoc record = null; // name of folder in which to record mail
71
boolean debug = false;
72     BufferedReader in =
73             new BufferedReader(new InputStreamReader(System.in));
74     int optind;
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("-s")) {
90         subject = argv[++optind];
91         } else if (argv[optind].equals("-o")) { // originator
92
from = argv[++optind];
93         } else if (argv[optind].equals("-c")) {
94         cc = argv[++optind];
95         } else if (argv[optind].equals("-b")) {
96         bcc = argv[++optind];
97         } else if (argv[optind].equals("-L")) {
98         url = argv[++optind];
99         } else if (argv[optind].equals("-d")) {
100         debug = true;
101         } else if (argv[optind].equals("--")) {
102         optind++;
103         break;
104         } else if (argv[optind].startsWith("-")) {
105         System.out.println(
106 "Usage: sendhtml [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
107         System.out.println(
108 "\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]");
109         System.out.println(
110 "\t[-f record-mailbox] [-M transport-host] [-d] [address]");
111         System.exit(1);
112         } else {
113         break;
114         }
115     }
116
117     try {
118         if (optind < argv.length) {
119         // XXX - concatenate all remaining arguments
120
to = argv[optind];
121         System.out.println("To: " + to);
122         } else {
123         System.out.print("To: ");
124         System.out.flush();
125         to = in.readLine();
126         }
127         if (subject == null) {
128         System.out.print("Subject: ");
129         System.out.flush();
130         subject = in.readLine();
131         } else {
132         System.out.println("Subject: " + subject);
133         }
134
135         Properties JavaDoc props = System.getProperties();
136         // XXX - could use Session.getTransport() and Transport.connect()
137
// XXX - assume we're using SMTP
138
if (mailhost != null)
139         props.put("mail.smtp.host", mailhost);
140
141         // Get a Session object
142
Session session = Session.getInstance(props, null);
143         if (debug)
144         session.setDebug(true);
145
146         // construct the message
147
Message msg = new MimeMessage(session);
148         if (from != null)
149         msg.setFrom(new InternetAddress(from));
150         else
151         msg.setFrom();
152
153         msg.setRecipients(Message.RecipientType.TO,
154                     InternetAddress.parse(to, false));
155         if (cc != null)
156         msg.setRecipients(Message.RecipientType.CC,
157                     InternetAddress.parse(cc, false));
158         if (bcc != null)
159         msg.setRecipients(Message.RecipientType.BCC,
160                     InternetAddress.parse(bcc, false));
161
162         msg.setSubject(subject);
163
164         collect(in, msg);
165
166         msg.setHeader("X-Mailer", mailer);
167         msg.setSentDate(new Date JavaDoc());
168
169         // send the thing off
170
Transport.send(msg);
171
172         System.out.println("\nMail was sent successfully.");
173
174         // Keep a copy, if requested.
175

176         if (record != null) {
177         // Get a Store object
178
Store store = null;
179         if (url != null) {
180             URLName urln = new URLName(url);
181             store = session.getStore(urln);
182             store.connect();
183         } else {
184             if (protocol != null)
185             store = session.getStore(protocol);
186             else
187             store = session.getStore();
188
189             // Connect
190
if (host != null || user != null || password != null)
191             store.connect(host, user, password);
192             else
193             store.connect();
194         }
195
196         // Get record Folder. Create if it does not exist.
197
Folder folder = store.getFolder(record);
198         if (folder == null) {
199             System.err.println("Can't get record folder.");
200             System.exit(1);
201         }
202         if (!folder.exists())
203             folder.create(Folder.HOLDS_MESSAGES);
204
205         Message[] msgs = new Message[1];
206         msgs[0] = msg;
207         folder.appendMessages(msgs);
208
209         System.out.println("Mail was recorded successfully.");
210         }
211
212     } catch (Exception JavaDoc e) {
213         e.printStackTrace();
214     }
215     }
216
217     public void collect(BufferedReader in, Message msg)
218                     throws MessagingException, IOException {
219     String JavaDoc line;
220     String JavaDoc subject = msg.getSubject();
221     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
222     sb.append("<HTML>\n");
223     sb.append("<HEAD>\n");
224     sb.append("<TITLE>\n");
225     sb.append(subject + "\n");
226     sb.append("</TITLE>\n");
227     sb.append("</HEAD>\n");
228
229     sb.append("<BODY>\n");
230     sb.append("<H1>" + subject + "</H1>" + "\n");
231
232     while ((line = in.readLine()) != null) {
233         sb.append(line);
234         sb.append("\n");
235     }
236
237     sb.append("</BODY>\n");
238     sb.append("</HTML>\n");
239
240     msg.setDataHandler(new DataHandler(
241         new ByteArrayDataSource(sb.toString(), "text/html")));
242     }
243 }
244
Popular Tags