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 org.apache.commons.net.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 to the specified recipients. It prompts you for header information and a file name 038 * containing the message. 039 */ 040 041public final class SMTPMail { 042 043 public static void main(final String[] args) { 044 final String sender; 045 final String recipient; 046 final String subject; 047 final String fileName; 048 final String server; 049 String cc; 050 final List<String> ccList = new ArrayList<>(); 051 final BufferedReader stdin; 052 FileReader fileReader = null; 053 final Writer writer; 054 final SimpleSMTPHeader header; 055 final SMTPClient client; 056 057 if (args.length < 1) { 058 System.err.println("Usage: SMTPMail <smtpserver>"); 059 System.exit(1); 060 } 061 062 server = args[0]; 063 064 stdin = new BufferedReader(new InputStreamReader(System.in)); 065 066 try { 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 while (true) { 085 System.out.print("CC <enter one address per line, hit enter to end>: "); 086 System.out.flush(); 087 088 cc = stdin.readLine(); 089 090 if (cc == null || cc.isEmpty()) { 091 break; 092 } 093 094 header.addCC(cc.trim()); 095 ccList.add(cc.trim()); 096 } 097 098 System.out.print("Filename: "); 099 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}