001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package examples;
017:
018: import java.io.BufferedReader;
019: import java.io.FileNotFoundException;
020: import java.io.FileReader;
021: import java.io.IOException;
022: import java.io.InputStreamReader;
023: import java.io.PrintWriter;
024: import java.io.Writer;
025: import java.util.Enumeration;
026: import java.util.Vector;
027: import org.apache.commons.net.io.Util;
028: import org.apache.commons.net.smtp.SMTPClient;
029: import org.apache.commons.net.smtp.SMTPReply;
030: import org.apache.commons.net.smtp.SimpleSMTPHeader;
031:
032: /***
033: * This is an example program using the SMTP package to send a message
034: * to the specified recipients. It prompts you for header information and
035: * a filename containing the message.
036: * <p>
037: ***/
038:
039: public final class mail {
040:
041: public final static void main(String[] args) {
042: String sender, recipient, subject, filename, server, cc;
043: Vector ccList = new Vector();
044: BufferedReader stdin;
045: FileReader fileReader = null;
046: Writer writer;
047: SimpleSMTPHeader header;
048: SMTPClient client;
049: Enumeration en;
050:
051: if (args.length < 1) {
052: System.err.println("Usage: mail smtpserver");
053: System.exit(1);
054: }
055:
056: server = args[0];
057:
058: stdin = new BufferedReader(new InputStreamReader(System.in));
059:
060: try {
061: System.out.print("From: ");
062: System.out.flush();
063:
064: sender = stdin.readLine();
065:
066: System.out.print("To: ");
067: System.out.flush();
068:
069: recipient = stdin.readLine();
070:
071: System.out.print("Subject: ");
072: System.out.flush();
073:
074: subject = stdin.readLine();
075:
076: header = new SimpleSMTPHeader(sender, recipient, subject);
077:
078: while (true) {
079: System.out
080: .print("CC <enter one address per line, hit enter to end>: ");
081: System.out.flush();
082:
083: // Of course you don't want to do this because readLine() may be null
084: cc = stdin.readLine().trim();
085:
086: if (cc.length() == 0)
087: break;
088:
089: header.addCC(cc);
090: ccList.addElement(cc);
091: }
092:
093: System.out.print("Filename: ");
094: System.out.flush();
095:
096: filename = stdin.readLine();
097:
098: try {
099: fileReader = new FileReader(filename);
100: } catch (FileNotFoundException e) {
101: System.err.println("File not found. " + e.getMessage());
102: }
103:
104: client = new SMTPClient();
105: client.addProtocolCommandListener(new PrintCommandListener(
106: new PrintWriter(System.out)));
107:
108: client.connect(server);
109:
110: if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
111: client.disconnect();
112: System.err.println("SMTP server refused connection.");
113: System.exit(1);
114: }
115:
116: client.login();
117:
118: client.setSender(sender);
119: client.addRecipient(recipient);
120:
121: en = ccList.elements();
122:
123: while (en.hasMoreElements())
124: client.addRecipient((String) en.nextElement());
125:
126: writer = client.sendMessageData();
127:
128: if (writer != null) {
129: writer.write(header.toString());
130: Util.copyReader(fileReader, writer);
131: writer.close();
132: client.completePendingCommand();
133: }
134:
135: fileReader.close();
136:
137: client.logout();
138:
139: client.disconnect();
140: } catch (IOException e) {
141: e.printStackTrace();
142: System.exit(1);
143: }
144: }
145: }
|