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    *      https://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 org.apache.commons.net.examples.mail;
19  
20  import java.io.BufferedReader;
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.io.InputStreamReader;
24  import java.io.Writer;
25  import java.nio.charset.Charset;
26  import java.nio.file.Files;
27  import java.nio.file.Paths;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import org.apache.commons.io.IOUtils;
32  import org.apache.commons.net.examples.PrintCommandListeners;
33  import org.apache.commons.net.smtp.SMTPClient;
34  import org.apache.commons.net.smtp.SMTPReply;
35  import org.apache.commons.net.smtp.SimpleSMTPHeader;
36  
37  /**
38   * This is an example program using the SMTP package to send a message to the specified recipients. It prompts you for header information and a file name
39   * containing the message.
40   */
41  
42  public final class SMTPMail {
43  
44      public static void main(final String[] args) {
45          final String sender;
46          final String recipient;
47          final String subject;
48          final String fileName;
49          final String server;
50          String cc;
51          final List<String> ccList = new ArrayList<>();
52          final BufferedReader stdin;
53          BufferedReader fileReader = null;
54          final Writer writer;
55          final SimpleSMTPHeader header;
56          final SMTPClient client;
57  
58          if (args.length < 1) {
59              System.err.println("Usage: SMTPMail <smtpserver>");
60              System.exit(1);
61          }
62  
63          server = args[0];
64  
65          stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
66  
67          try {
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              while (true) {
86                  System.out.print("CC <enter one address per line, hit enter to end>: ");
87                  System.out.flush();
88  
89                  cc = stdin.readLine();
90  
91                  if (cc == null || cc.isEmpty()) {
92                      break;
93                  }
94  
95                  header.addCC(cc.trim());
96                  ccList.add(cc.trim());
97              }
98  
99              System.out.print("Filename: ");
100             System.out.flush();
101 
102             fileName = stdin.readLine();
103 
104             try {
105                 fileReader = Files.newBufferedReader(Paths.get(fileName), Charset.defaultCharset());
106             } catch (final FileNotFoundException e) {
107                 System.err.println("File not found. " + e.getMessage());
108             }
109 
110             client = new SMTPClient();
111             client.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener());
112 
113             client.connect(server);
114 
115             if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
116                 client.disconnect();
117                 System.err.println("SMTP server refused connection.");
118                 System.exit(1);
119             }
120 
121             client.login();
122 
123             client.setSender(sender);
124             client.addRecipient(recipient);
125 
126             for (final String recpt : ccList) {
127                 client.addRecipient(recpt);
128             }
129 
130             writer = client.sendMessageData();
131 
132             if (writer != null) {
133                 writer.write(header.toString());
134                 IOUtils.copyLarge(fileReader, writer);
135                 writer.close();
136                 client.completePendingCommand();
137             }
138             IOUtils.close(fileReader);
139             client.logout();
140             client.disconnect();
141         } catch (final IOException e) {
142             e.printStackTrace();
143             System.exit(1);
144         }
145     }
146 }