blob: d939ce5515b9425f8cc05fb2ab1c31cbf360d592 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.net.www.protocol.mailto;
27
28import java.net.URL;
29import java.net.InetAddress;
30import java.net.SocketPermission;
31import java.io.*;
32import java.util.Enumeration;
33import java.util.Hashtable;
34import java.util.StringTokenizer;
35import java.security.Permission;
36import sun.net.www.*;
37import sun.net.smtp.SmtpClient;
38import sun.net.www.ParseUtil;
39
40
41/**
42 * Handle mailto URLs. To send mail using a mailto URLConnection,
43 * call <code>getOutputStream</code>, write the message to the output
44 * stream, and close it.
45 *
46 */
47public class MailToURLConnection extends URLConnection {
48 InputStream is = null;
49 OutputStream os = null;
50
51 SmtpClient client;
52 Permission permission;
53 private int connectTimeout = -1;
54 private int readTimeout = -1;
55
56 MailToURLConnection(URL u) {
57 super(u);
58
59 MessageHeader props = new MessageHeader();
60 props.add("content-type", "text/html");
61 setProperties(props);
62 }
63
64 /**
65 * Get the user's full email address - stolen from
66 * HotJavaApplet.getMailAddress().
67 */
68 String getFromAddress() {
69 String str = System.getProperty("user.fromaddr");
70 if (str == null) {
71 str = System.getProperty("user.name");
72 if (str != null) {
73 String host = System.getProperty("mail.host");
74 if (host == null) {
75 try {
76 host = InetAddress.getLocalHost().getHostName();
77 } catch (java.net.UnknownHostException e) {
78 }
79 }
80 str += "@" + host;
81 } else {
82 str = "";
83 }
84 }
85 return str;
86 }
87
88 public void connect() throws IOException {
89 System.err.println("connect. Timeout = " + connectTimeout);
90 client = new SmtpClient(connectTimeout);
91 client.setReadTimeout(readTimeout);
92 }
93
94 public synchronized OutputStream getOutputStream() throws IOException {
95 if (os != null) {
96 return os;
97 } else if (is != null) {
98 throw new IOException("Cannot write output after reading input.");
99 }
100 connect();
101
102 String to = ParseUtil.decode(url.getPath());
103 client.from(getFromAddress());
104 client.to(to);
105
106 os = client.startMessage();
107 return os;
108 }
109
110 public Permission getPermission() throws IOException {
111 if (permission == null) {
112 connect();
113 String host = client.getMailHost() + ":" + 25;
114 permission = new SocketPermission(host, "connect");
115 }
116 return permission;
117 }
118
119 public void setConnectTimeout(int timeout) {
120 if (timeout < 0)
121 throw new IllegalArgumentException("timeouts can't be negative");
122 connectTimeout = timeout;
123 }
124
125 public int getConnectTimeout() {
126 return (connectTimeout < 0 ? 0 : connectTimeout);
127 }
128
129 public void setReadTimeout(int timeout) {
130 if (timeout < 0)
131 throw new IllegalArgumentException("timeouts can't be negative");
132 readTimeout = timeout;
133 }
134
135 public int getReadTimeout() {
136 return readTimeout < 0 ? 0 : readTimeout;
137 }
138}