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