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.io.Util;
34 import org.apache.commons.net.smtp.SMTPClient;
35 import org.apache.commons.net.smtp.SMTPReply;
36 import org.apache.commons.net.smtp.SimpleSMTPHeader;
37
38
39
40
41
42
43 public final class SMTPMail {
44
45 public static void main(final String[] args) {
46 final String sender;
47 final String recipient;
48 final String subject;
49 final String fileName;
50 final String server;
51 String cc;
52 final List<String> ccList = new ArrayList<>();
53 final BufferedReader stdin;
54 BufferedReader fileReader = null;
55 final Writer writer;
56 final SimpleSMTPHeader header;
57 final SMTPClient client;
58
59 if (args.length < 1) {
60 System.err.println("Usage: SMTPMail <smtpserver>");
61 System.exit(1);
62 }
63
64 server = args[0];
65
66 stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
67
68 try {
69 System.out.print("From: ");
70 System.out.flush();
71
72 sender = stdin.readLine();
73
74 System.out.print("To: ");
75 System.out.flush();
76
77 recipient = stdin.readLine();
78
79 System.out.print("Subject: ");
80 System.out.flush();
81
82 subject = stdin.readLine();
83
84 header = new SimpleSMTPHeader(sender, recipient, subject);
85
86 while (true) {
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.isEmpty()) {
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 fileReader = Files.newBufferedReader(Paths.get(fileName), Charset.defaultCharset());
107 } catch (final FileNotFoundException e) {
108 System.err.println("File not found. " + e.getMessage());
109 }
110
111 client = new SMTPClient();
112 client.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener());
113
114 client.connect(server);
115
116 if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
117 client.disconnect();
118 System.err.println("SMTP server refused connection.");
119 System.exit(1);
120 }
121
122 client.login();
123
124 client.setSender(sender);
125 client.addRecipient(recipient);
126
127 for (final String recpt : ccList) {
128 client.addRecipient(recpt);
129 }
130
131 writer = client.sendMessageData();
132
133 if (writer != null) {
134 writer.write(header.toString());
135 Util.copyReader(fileReader, writer);
136 writer.close();
137 client.completePendingCommand();
138 }
139 IOUtils.close(fileReader);
140 client.logout();
141 client.disconnect();
142 } catch (final IOException e) {
143 e.printStackTrace();
144 System.exit(1);
145 }
146 }
147 }