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