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