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.mail;
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.ArrayList;
28  import java.util.List;
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 SMTPMail
44  {
45  
46      public static void main(String[] args)
47      {
48          String sender, recipient, subject, filename, server, cc;
49          List<String> ccList = new ArrayList<String>();
50          BufferedReader stdin;
51          FileReader fileReader = null;
52          Writer writer;
53          SimpleSMTPHeader header;
54          SMTPClient client;
55  
56          if (args.length < 1)
57          {
58              System.err.println("Usage: mail smtpserver");
59              System.exit(1);
60          }
61  
62          server = args[0];
63  
64          stdin = new BufferedReader(new InputStreamReader(System.in));
65  
66          try
67          {
68              System.out.print("From: ");
69              System.out.flush();
70  
71              sender = stdin.readLine();
72  
73              System.out.print("To: ");
74              System.out.flush();
75  
76              recipient = stdin.readLine();
77  
78              System.out.print("Subject: ");
79              System.out.flush();
80  
81              subject = stdin.readLine();
82  
83              header = new SimpleSMTPHeader(sender, recipient, subject);
84  
85  
86              while (true)
87              {
88                  System.out.print("CC <enter one address per line, hit enter to end>: ");
89                  System.out.flush();
90  
91                  cc = stdin.readLine();
92  
93                  if (cc== null || cc.length() == 0) {
94                      break;
95                  }
96  
97                  header.addCC(cc.trim());
98                  ccList.add(cc.trim());
99              }
100 
101             System.out.print("Filename: ");
102             System.out.flush();
103 
104             filename = stdin.readLine();
105 
106             try
107             {
108                 fileReader = new FileReader(filename);
109             }
110             catch (FileNotFoundException e)
111             {
112                 System.err.println("File not found. " + e.getMessage());
113             }
114 
115             client = new SMTPClient();
116             client.addProtocolCommandListener(new PrintCommandListener(
117                                                   new PrintWriter(System.out), true));
118 
119             client.connect(server);
120 
121             if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
122             {
123                 client.disconnect();
124                 System.err.println("SMTP server refused connection.");
125                 System.exit(1);
126             }
127 
128             client.login();
129 
130             client.setSender(sender);
131             client.addRecipient(recipient);
132 
133 
134 
135             for (String recpt : ccList) {
136                 client.addRecipient(recpt);
137             }
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             if (fileReader != null ) {
150                 fileReader.close();
151             }
152 
153             client.logout();
154 
155             client.disconnect();
156         }
157         catch (IOException e)
158         {
159             e.printStackTrace();
160             System.exit(1);
161         }
162     }
163 }
164 
165