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  
18  package examples;
19  
20  import java.io.BufferedReader;
21  import java.io.FileNotFoundException;
22  import java.io.FileReader;
23  import java.io.IOException;
24  import java.io.InputStreamReader;
25  import java.io.PrintWriter;
26  import java.io.Writer;
27  import java.util.Enumeration;
28  import java.util.Vector;
29  
30  import org.apache.commons.net.PrintCommandListener;
31  import org.apache.commons.net.io.Util;
32  import org.apache.commons.net.smtp.SMTPClient;
33  import org.apache.commons.net.smtp.SMTPReply;
34  import org.apache.commons.net.smtp.SimpleSMTPHeader;
35  
36  /***
37   * This is an example program using the SMTP package to send a message
38   * to the specified recipients.  It prompts you for header information and
39   * a filename containing the message.
40   * <p>
41   ***/
42  
43  public final class mail
44  {
45  
46      public final static void main(String[] args)
47      {
48          String sender, recipient, subject, filename, server, cc;
49          Vector ccList = new Vector();
50          BufferedReader stdin;
51          FileReader fileReader = null;
52          Writer writer;
53          SimpleSMTPHeader header;
54          SMTPClient client;
55          Enumeration en;
56  
57          if (args.length < 1)
58          {
59              System.err.println("Usage: mail smtpserver");
60              System.exit(1);
61          }
62  
63          server = args[0];
64  
65          stdin = new BufferedReader(new InputStreamReader(System.in));
66  
67          try
68          {
69              System.out.print("From: ");
70              System.out.flush();
71  
72              sender = stdin.readLine();
73  
74              System.out.print("To: ");
75              System.out.flush();
76  
77              recipient = stdin.readLine();
78  
79              System.out.print("Subject: ");
80              System.out.flush();
81  
82              subject = stdin.readLine();
83  
84              header = new SimpleSMTPHeader(sender, recipient, subject);
85  
86  
87              while (true)
88              {
89                  System.out.print("CC <enter one address per line, hit enter to end>: ");
90                  System.out.flush();
91  
92                  // Of course you don't want to do this because readLine() may be null
93                  cc = stdin.readLine().trim();
94  
95                  if (cc.length() == 0)
96                      break;
97  
98                  header.addCC(cc);
99                  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