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.  *      https://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.io.IOUtils;
  29. import org.apache.commons.net.examples.PrintCommandListeners;
  30. import org.apache.commons.net.io.Util;
  31. import org.apache.commons.net.smtp.SMTPClient;
  32. import org.apache.commons.net.smtp.SMTPReply;
  33. import org.apache.commons.net.smtp.SimpleSMTPHeader;

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

  38. public final class SMTPMail {

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

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

  56.         server = args[0];

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

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

  61.             sender = stdin.readLine();

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

  64.             recipient = stdin.readLine();

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

  67.             subject = stdin.readLine();

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

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

  72.                 cc = stdin.readLine();

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

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

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

  81.             fileName = stdin.readLine();

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

  87.             client = new SMTPClient();
  88.             client.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener());

  89.             client.connect(server);

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

  95.             client.login();

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

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

  101.             writer = client.sendMessageData();

  102.             if (writer != null) {
  103.                 writer.write(header.toString());
  104.                 Util.copyReader(fileReader, writer);
  105.                 writer.close();
  106.                 client.completePendingCommand();
  107.             }
  108.             IOUtils.close(fileReader);
  109.             client.logout();
  110.             client.disconnect();
  111.         } catch (final IOException e) {
  112.             e.printStackTrace();
  113.             System.exit(1);
  114.         }
  115.     }
  116. }