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