View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package examples;
18  
19  import java.io.BufferedReader;
20  import java.io.FileNotFoundException;
21  import java.io.FileReader;
22  import java.io.IOException;
23  import java.io.InputStreamReader;
24  import java.io.PrintWriter;
25  import java.io.Writer;
26  import java.util.Enumeration;
27  import java.util.Vector;
28  import org.apache.commons.net.io.Util;
29  import org.apache.commons.net.smtp.SMTPClient;
30  import org.apache.commons.net.smtp.SMTPReply;
31  import org.apache.commons.net.smtp.SimpleSMTPHeader;
32  
33  /***
34   * This is an example program using the SMTP package to send a message
35   * to the specified recipients.  It prompts you for header information and
36   * a filename containing the message.
37   * <p>
38   ***/
39  
40  public final class mail
41  {
42  
43      public final static void main(String[] args)
44      {
45          String sender, recipient, subject, filename, server, cc;
46          Vector ccList = new Vector();
47          BufferedReader stdin;
48          FileReader fileReader = null;
49          Writer writer;
50          SimpleSMTPHeader header;
51          SMTPClient client;
52          Enumeration en;
53  
54          if (args.length < 1)
55          {
56              System.err.println("Usage: mail smtpserver");
57              System.exit(1);
58          }
59  
60          server = args[0];
61  
62          stdin = new BufferedReader(new InputStreamReader(System.in));
63  
64          try
65          {
66              System.out.print("From: ");
67              System.out.flush();
68  
69              sender = stdin.readLine();
70  
71              System.out.print("To: ");
72              System.out.flush();
73  
74              recipient = stdin.readLine();
75  
76              System.out.print("Subject: ");
77              System.out.flush();
78  
79              subject = stdin.readLine();
80  
81              header = new SimpleSMTPHeader(sender, recipient, subject);
82  
83  
84              while (true)
85              {
86                  System.out.print("CC <enter one address per line, hit enter to end>: ");
87                  System.out.flush();
88  
89                  // Of course you don't want to do this because readLine() may be null
90                  cc = stdin.readLine().trim();
91  
92                  if (cc.length() == 0)
93                      break;
94  
95                  header.addCC(cc);
96                  ccList.addElement(cc);
97              }
98  
99              System.out.print("Filename: ");
100             System.out.flush();
101 
102             filename = stdin.readLine();
103 
104             try
105             {
106                 fileReader = new FileReader(filename);
107             }
108             catch (FileNotFoundException e)
109             {
110                 System.err.println("File not found. " + e.getMessage());
111             }
112 
113             client = new SMTPClient();
114             client.addProtocolCommandListener(new PrintCommandListener(
115                                                   new PrintWriter(System.out)));
116 
117             client.connect(server);
118 
119             if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
120             {
121                 client.disconnect();
122                 System.err.println("SMTP server refused connection.");
123                 System.exit(1);
124             }
125 
126             client.login();
127 
128             client.setSender(sender);
129             client.addRecipient(recipient);
130 
131             en = ccList.elements();
132 
133             while (en.hasMoreElements())
134                 client.addRecipient((String)en.nextElement());
135 
136             writer = client.sendMessageData();
137 
138             if (writer != null)
139             {
140                 writer.write(header.toString());
141                 Util.copyReader(fileReader, writer);
142                 writer.close();
143                 client.completePendingCommand();
144             }
145 
146             fileReader.close();
147 
148             client.logout();
149 
150             client.disconnect();
151         }
152         catch (IOException e)
153         {
154             e.printStackTrace();
155             System.exit(1);
156         }
157     }
158 }
159 
160