SMTPMail.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.examples.mail;

  18. import java.io.BufferedReader;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.io.Writer;
  23. import java.nio.charset.Charset;
  24. import java.nio.file.Files;
  25. import java.nio.file.Paths;
  26. import java.util.ArrayList;
  27. import java.util.List;

  28. import org.apache.commons.net.PrintCommandListener;
  29. import org.apache.commons.net.io.Util;
  30. import org.apache.commons.net.smtp.SMTPClient;
  31. import org.apache.commons.net.smtp.SMTPReply;
  32. import org.apache.commons.net.smtp.SimpleSMTPHeader;

  33. /**
  34.  * 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
  35.  * containing the message.
  36.  */

  37. public final class SMTPMail {

  38.     public static void main(final String[] args) {
  39.         final String sender;
  40.         final String recipient;
  41.         final String subject;
  42.         final String fileName;
  43.         final String server;
  44.         String cc;
  45.         final List<String> ccList = new ArrayList<>();
  46.         final BufferedReader stdin;
  47.         BufferedReader fileReader = null;
  48.         final Writer writer;
  49.         final SimpleSMTPHeader header;
  50.         final SMTPClient client;

  51.         if (args.length < 1) {
  52.             System.err.println("Usage: SMTPMail <smtpserver>");
  53.             System.exit(1);
  54.         }

  55.         server = args[0];

  56.         stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));

  57.         try {
  58.             System.out.print("From: ");
  59.             System.out.flush();

  60.             sender = stdin.readLine();

  61.             System.out.print("To: ");
  62.             System.out.flush();

  63.             recipient = stdin.readLine();

  64.             System.out.print("Subject: ");
  65.             System.out.flush();

  66.             subject = stdin.readLine();

  67.             header = new SimpleSMTPHeader(sender, recipient, subject);

  68.             while (true) {
  69.                 System.out.print("CC <enter one address per line, hit enter to end>: ");
  70.                 System.out.flush();

  71.                 cc = stdin.readLine();

  72.                 if (cc == null || cc.isEmpty()) {
  73.                     break;
  74.                 }

  75.                 header.addCC(cc.trim());
  76.                 ccList.add(cc.trim());
  77.             }

  78.             System.out.print("Filename: ");
  79.             System.out.flush();

  80.             fileName = stdin.readLine();

  81.             try {
  82.                 fileReader = Files.newBufferedReader(Paths.get(fileName), Charset.defaultCharset());
  83.             } catch (final FileNotFoundException e) {
  84.                 System.err.println("File not found. " + e.getMessage());
  85.             }

  86.             client = new SMTPClient();
  87.             client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));

  88.             client.connect(server);

  89.             if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
  90.                 client.disconnect();
  91.                 System.err.println("SMTP server refused connection.");
  92.                 System.exit(1);
  93.             }

  94.             client.login();

  95.             client.setSender(sender);
  96.             client.addRecipient(recipient);

  97.             for (final String recpt : ccList) {
  98.                 client.addRecipient(recpt);
  99.             }

  100.             writer = client.sendMessageData();

  101.             if (writer != null) {
  102.                 writer.write(header.toString());
  103.                 Util.copyReader(fileReader, writer);
  104.                 writer.close();
  105.                 client.completePendingCommand();
  106.             }

  107.             if (fileReader != null) {
  108.                 fileReader.close();
  109.             }

  110.             client.logout();

  111.             client.disconnect();
  112.         } catch (final IOException e) {
  113.             e.printStackTrace();
  114.             System.exit(1);
  115.         }
  116.     }
  117. }