1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
39
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 }