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