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