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 018package examples.mail; 019 020import java.io.BufferedReader; 021import java.io.FileNotFoundException; 022import java.io.FileReader; 023import java.io.IOException; 024import java.io.InputStreamReader; 025import java.io.PrintWriter; 026import java.io.Writer; 027import java.util.ArrayList; 028import java.util.List; 029 030import org.apache.commons.net.PrintCommandListener; 031import org.apache.commons.net.io.Util; 032import org.apache.commons.net.smtp.SMTPClient; 033import org.apache.commons.net.smtp.SMTPReply; 034import org.apache.commons.net.smtp.SimpleSMTPHeader; 035 036/*** 037 * This is an example program using the SMTP package to send a message 038 * to the specified recipients. It prompts you for header information and 039 * a filename containing the message. 040 ***/ 041 042public final class SMTPMail 043{ 044 045 public static void main(String[] args) 046 { 047 String sender, recipient, subject, filename, server, cc; 048 List<String> ccList = new ArrayList<String>(); 049 BufferedReader stdin; 050 FileReader fileReader = null; 051 Writer writer; 052 SimpleSMTPHeader header; 053 SMTPClient client; 054 055 if (args.length < 1) 056 { 057 System.err.println("Usage: mail smtpserver"); 058 System.exit(1); 059 } 060 061 server = args[0]; 062 063 stdin = new BufferedReader(new InputStreamReader(System.in)); 064 065 try 066 { 067 System.out.print("From: "); 068 System.out.flush(); 069 070 sender = stdin.readLine(); 071 072 System.out.print("To: "); 073 System.out.flush(); 074 075 recipient = stdin.readLine(); 076 077 System.out.print("Subject: "); 078 System.out.flush(); 079 080 subject = stdin.readLine(); 081 082 header = new SimpleSMTPHeader(sender, recipient, subject); 083 084 085 while (true) 086 { 087 System.out.print("CC <enter one address per line, hit enter to end>: "); 088 System.out.flush(); 089 090 cc = stdin.readLine(); 091 092 if (cc== null || cc.length() == 0) { 093 break; 094 } 095 096 header.addCC(cc.trim()); 097 ccList.add(cc.trim()); 098 } 099 100 System.out.print("Filename: "); 101 System.out.flush(); 102 103 filename = stdin.readLine(); 104 105 try 106 { 107 fileReader = new FileReader(filename); 108 } 109 catch (FileNotFoundException e) 110 { 111 System.err.println("File not found. " + e.getMessage()); 112 } 113 114 client = new SMTPClient(); 115 client.addProtocolCommandListener(new PrintCommandListener( 116 new PrintWriter(System.out), true)); 117 118 client.connect(server); 119 120 if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) 121 { 122 client.disconnect(); 123 System.err.println("SMTP server refused connection."); 124 System.exit(1); 125 } 126 127 client.login(); 128 129 client.setSender(sender); 130 client.addRecipient(recipient); 131 132 133 134 for (String recpt : ccList) { 135 client.addRecipient(recpt); 136 } 137 138 writer = client.sendMessageData(); 139 140 if (writer != null) 141 { 142 writer.write(header.toString()); 143 Util.copyReader(fileReader, writer); 144 writer.close(); 145 client.completePendingCommand(); 146 } 147 148 if (fileReader != null ) { 149 fileReader.close(); 150 } 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