1 package examples;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Commons" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.BufferedReader;
58 import java.io.FileNotFoundException;
59 import java.io.FileReader;
60 import java.io.IOException;
61 import java.io.InputStreamReader;
62 import java.io.PrintWriter;
63 import java.io.Writer;
64 import java.util.Enumeration;
65 import java.util.Vector;
66 import org.apache.commons.net.io.Util;
67 import org.apache.commons.net.smtp.SMTPClient;
68 import org.apache.commons.net.smtp.SMTPReply;
69 import org.apache.commons.net.smtp.SimpleSMTPHeader;
70
71 /****
72 * This is an example program using the SMTP package to send a message
73 * to the specified recipients. It prompts you for header information and
74 * a filename containing the message.
75 * <p>
76 ***/
77
78 public final class mail
79 {
80
81 public final static void main(String[] args)
82 {
83 String sender, recipient, subject, filename, server, cc;
84 Vector ccList = new Vector();
85 BufferedReader stdin;
86 FileReader fileReader = null;
87 Writer writer;
88 SimpleSMTPHeader header;
89 SMTPClient client;
90 Enumeration enum;
91
92 if (args.length < 1)
93 {
94 System.err.println("Usage: mail smtpserver");
95 System.exit(1);
96 }
97
98 server = args[0];
99
100 stdin = new BufferedReader(new InputStreamReader(System.in));
101
102 try
103 {
104 System.out.print("From: ");
105 System.out.flush();
106
107 sender = stdin.readLine();
108
109 System.out.print("To: ");
110 System.out.flush();
111
112 recipient = stdin.readLine();
113
114 System.out.print("Subject: ");
115 System.out.flush();
116
117 subject = stdin.readLine();
118
119 header = new SimpleSMTPHeader(sender, recipient, subject);
120
121
122 while (true)
123 {
124 System.out.print("CC <enter one address per line, hit enter to end>: ");
125 System.out.flush();
126
127 // Of course you don't want to do this because readLine() may be null
128 cc = stdin.readLine().trim();
129
130 if (cc.length() == 0)
131 break;
132
133 header.addCC(cc);
134 ccList.addElement(cc);
135 }
136
137 System.out.print("Filename: ");
138 System.out.flush();
139
140 filename = stdin.readLine();
141
142 try
143 {
144 fileReader = new FileReader(filename);
145 }
146 catch (FileNotFoundException e)
147 {
148 System.err.println("File not found. " + e.getMessage());
149 }
150
151 client = new SMTPClient();
152 client.addProtocolCommandListener(new PrintCommandListener(
153 new PrintWriter(System.out)));
154
155 client.connect(server);
156
157 if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
158 {
159 client.disconnect();
160 System.err.println("SMTP server refused connection.");
161 System.exit(1);
162 }
163
164 client.login();
165
166 client.setSender(sender);
167 client.addRecipient(recipient);
168
169 enum = ccList.elements();
170
171 while (enum.hasMoreElements())
172 client.addRecipient((String)enum.nextElement());
173
174 writer = client.sendMessageData();
175
176 if (writer != null)
177 {
178 writer.write(header.toString());
179 Util.copyReader(fileReader, writer);
180 writer.close();
181 client.completePendingCommand();
182 }
183
184 fileReader.close();
185
186 client.logout();
187
188 client.disconnect();
189 }
190 catch (IOException e)
191 {
192 e.printStackTrace();
193 System.exit(1);
194 }
195 }
196 }
197
198
This page was automatically generated by Maven