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