001 /*
002 * Copyright 2001-2005 The Apache Software Foundation
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package examples;
017
018 import java.io.BufferedReader;
019 import java.io.FileNotFoundException;
020 import java.io.FileReader;
021 import java.io.IOException;
022 import java.io.InputStreamReader;
023 import java.io.PrintWriter;
024 import java.io.Writer;
025 import java.util.Enumeration;
026 import java.util.Vector;
027 import org.apache.commons.net.io.Util;
028 import org.apache.commons.net.smtp.SMTPClient;
029 import org.apache.commons.net.smtp.SMTPReply;
030 import org.apache.commons.net.smtp.SimpleSMTPHeader;
031
032 /***
033 * This is an example program using the SMTP package to send a message
034 * to the specified recipients. It prompts you for header information and
035 * a filename containing the message.
036 * <p>
037 ***/
038
039 public final class mail
040 {
041
042 public final static void main(String[] args)
043 {
044 String sender, recipient, subject, filename, server, cc;
045 Vector ccList = new Vector();
046 BufferedReader stdin;
047 FileReader fileReader = null;
048 Writer writer;
049 SimpleSMTPHeader header;
050 SMTPClient client;
051 Enumeration en;
052
053 if (args.length < 1)
054 {
055 System.err.println("Usage: mail smtpserver");
056 System.exit(1);
057 }
058
059 server = args[0];
060
061 stdin = new BufferedReader(new InputStreamReader(System.in));
062
063 try
064 {
065 System.out.print("From: ");
066 System.out.flush();
067
068 sender = stdin.readLine();
069
070 System.out.print("To: ");
071 System.out.flush();
072
073 recipient = stdin.readLine();
074
075 System.out.print("Subject: ");
076 System.out.flush();
077
078 subject = stdin.readLine();
079
080 header = new SimpleSMTPHeader(sender, recipient, subject);
081
082
083 while (true)
084 {
085 System.out.print("CC <enter one address per line, hit enter to end>: ");
086 System.out.flush();
087
088 // Of course you don't want to do this because readLine() may be null
089 cc = stdin.readLine().trim();
090
091 if (cc.length() == 0)
092 break;
093
094 header.addCC(cc);
095 ccList.addElement(cc);
096 }
097
098 System.out.print("Filename: ");
099 System.out.flush();
100
101 filename = stdin.readLine();
102
103 try
104 {
105 fileReader = new FileReader(filename);
106 }
107 catch (FileNotFoundException e)
108 {
109 System.err.println("File not found. " + e.getMessage());
110 }
111
112 client = new SMTPClient();
113 client.addProtocolCommandListener(new PrintCommandListener(
114 new PrintWriter(System.out)));
115
116 client.connect(server);
117
118 if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
119 {
120 client.disconnect();
121 System.err.println("SMTP server refused connection.");
122 System.exit(1);
123 }
124
125 client.login();
126
127 client.setSender(sender);
128 client.addRecipient(recipient);
129
130 en = ccList.elements();
131
132 while (en.hasMoreElements())
133 client.addRecipient((String)en.nextElement());
134
135 writer = client.sendMessageData();
136
137 if (writer != null)
138 {
139 writer.write(header.toString());
140 Util.copyReader(fileReader, writer);
141 writer.close();
142 client.completePendingCommand();
143 }
144
145 fileReader.close();
146
147 client.logout();
148
149 client.disconnect();
150 }
151 catch (IOException e)
152 {
153 e.printStackTrace();
154 System.exit(1);
155 }
156 }
157 }
158
159