1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples;
17
18 import java.io.BufferedReader;
19 import java.io.FileNotFoundException;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.io.PrintWriter;
24 import java.io.Writer;
25 import java.util.Enumeration;
26 import java.util.Vector;
27 import org.apache.commons.net.io.Util;
28 import org.apache.commons.net.smtp.SMTPClient;
29 import org.apache.commons.net.smtp.SMTPReply;
30 import org.apache.commons.net.smtp.SimpleSMTPHeader;
31
32 /***
33 * This is an example program using the SMTP package to send a message
34 * to the specified recipients. It prompts you for header information and
35 * a filename containing the message.
36 * <p>
37 ***/
38
39 public final class mail
40 {
41
42 public final static void main(String[] args)
43 {
44 String sender, recipient, subject, filename, server, cc;
45 Vector ccList = new Vector();
46 BufferedReader stdin;
47 FileReader fileReader = null;
48 Writer writer;
49 SimpleSMTPHeader header;
50 SMTPClient client;
51 Enumeration en;
52
53 if (args.length < 1)
54 {
55 System.err.println("Usage: mail smtpserver");
56 System.exit(1);
57 }
58
59 server = args[0];
60
61 stdin = new BufferedReader(new InputStreamReader(System.in));
62
63 try
64 {
65 System.out.print("From: ");
66 System.out.flush();
67
68 sender = stdin.readLine();
69
70 System.out.print("To: ");
71 System.out.flush();
72
73 recipient = stdin.readLine();
74
75 System.out.print("Subject: ");
76 System.out.flush();
77
78 subject = stdin.readLine();
79
80 header = new SimpleSMTPHeader(sender, recipient, subject);
81
82
83 while (true)
84 {
85 System.out.print("CC <enter one address per line, hit enter to end>: ");
86 System.out.flush();
87
88
89 cc = stdin.readLine().trim();
90
91 if (cc.length() == 0)
92 break;
93
94 header.addCC(cc);
95 ccList.addElement(cc);
96 }
97
98 System.out.print("Filename: ");
99 System.out.flush();
100
101 filename = stdin.readLine();
102
103 try
104 {
105 fileReader = new FileReader(filename);
106 }
107 catch (FileNotFoundException e)
108 {
109 System.err.println("File not found. " + e.getMessage());
110 }
111
112 client = new SMTPClient();
113 client.addProtocolCommandListener(new PrintCommandListener(
114 new PrintWriter(System.out)));
115
116 client.connect(server);
117
118 if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
119 {
120 client.disconnect();
121 System.err.println("SMTP server refused connection.");
122 System.exit(1);
123 }
124
125 client.login();
126
127 client.setSender(sender);
128 client.addRecipient(recipient);
129
130 en = ccList.elements();
131
132 while (en.hasMoreElements())
133 client.addRecipient((String)en.nextElement());
134
135 writer = client.sendMessageData();
136
137 if (writer != null)
138 {
139 writer.write(header.toString());
140 Util.copyReader(fileReader, writer);
141 writer.close();
142 client.completePendingCommand();
143 }
144
145 fileReader.close();
146
147 client.logout();
148
149 client.disconnect();
150 }
151 catch (IOException e)
152 {
153 e.printStackTrace();
154 System.exit(1);
155 }
156 }
157 }
158
159