1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.skara.email; 24 25 import java.io.*; 26 import java.net.Socket; 27 import java.time.format.DateTimeFormatter; 28 import java.util.regex.Pattern; 29 30 /** 31 * Limited SMTP client implementation - only compatibility requirement (currently) is the OpenJDK 32 * mailing list servers. 33 */ 34 public class SMTP { 35 private static Pattern initReply = Pattern.compile("220 .*"); 36 private static Pattern ehloReply = Pattern.compile("^250 HELP$"); 37 private static Pattern mailReply = Pattern.compile("^250 .*"); 38 private static Pattern rcptReply = Pattern.compile("^250 .*"); 39 private static Pattern dataReply = Pattern.compile("354 Enter.*"); 40 private static Pattern doneReply = Pattern.compile("250 .*"); 41 42 public static void send(String server, EmailAddress recipient, Email email) throws IOException { 43 var port = 25; 44 if (server.contains(":")) { 45 var parts = server.split(":", 2); 46 server = parts[0]; 47 port = Integer.parseInt(parts[1]); 48 } 49 try (var socket = new Socket(server, port); 50 var out = new OutputStreamWriter(socket.getOutputStream()); 51 var in = new InputStreamReader(socket.getInputStream())) { 52 53 var session = new SMTPSession(in, out); 54 55 session.waitForPattern(initReply); 56 session.sendCommand("EHLO " + email.sender().domain(), ehloReply); 57 session.sendCommand("MAIL FROM:" + email.sender().address(), mailReply); 58 session.sendCommand("RCPT TO:<" + recipient.address() + ">", rcptReply); 59 session.sendCommand("DATA", dataReply); 60 session.sendCommand("From: " + email.author()); 61 session.sendCommand("Message-Id: " + email.id()); 62 session.sendCommand("Date: " + email.date().format(DateTimeFormatter.RFC_1123_DATE_TIME)); 63 session.sendCommand("Sender: " + email.sender()); 64 session.sendCommand("To: " + recipient); 65 for (var header : email.headers()) { 66 session.sendCommand(header + ": " + email.headerValue(header)); 67 } 68 session.sendCommand("Subject: " + email.subject()); 69 session.sendCommand(""); 70 session.sendCommand(email.body()); 71 session.sendCommand(".", doneReply); 72 session.sendCommand("QUIT"); 73 } 74 } 75 }