Skip to content
Permalink
Browse files

151: Encode single dot characters in the SMTP client

Reviewed-by: ehelin
  • Loading branch information
Robin Westberg
Robin Westberg committed Nov 7, 2019
1 parent f4cc08e commit b1e3d870862f9ef0b7adcb015fd5c3ae243c25ec
@@ -28,18 +28,19 @@
import java.time.Duration;
import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* 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 initReply = Pattern.compile("^220 .*");
private static Pattern ehloReply = Pattern.compile("^250 .*");
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 .*");
private static Pattern dataReply = Pattern.compile("^354 .*");
private static Pattern doneReply = Pattern.compile("^250 .*");

public static void send(String server, EmailAddress recipient, Email email) throws IOException {
send(server, recipient, email, Duration.ofMinutes(30));
@@ -74,7 +75,10 @@ public static void send(String server, EmailAddress recipient, Email email, Dura
session.sendCommand("Subject: " + MimeText.encode(email.subject()));
session.sendCommand("Content-type: text/plain; charset=utf-8");
session.sendCommand("");
session.sendCommand(email.body());
var escapedBody = email.body().lines()
.map(line -> line.startsWith(".") ? "." + line : line)
.collect(Collectors.joining("\n"));
session.sendCommand(escapedBody);
session.sendCommand(".", doneReply);
session.sendCommand("QUIT");
}
@@ -22,24 +22,19 @@
*/
package org.openjdk.skara.email;

import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.openjdk.skara.test.SMTPServer;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.*;

import java.io.IOException;
import java.time.Duration;
import java.util.logging.Logger;

import static org.junit.jupiter.api.Assertions.*;

class SMTPTests {
private final static Logger log = Logger.getLogger("org.openjdk.skara.email");;

@Test
void simple() throws IOException {
log.info("Hello");
try (var server = new SMTPServer()) {
var sender = EmailAddress.from("Test", "test@test.email");
var recipient = EmailAddress.from("Dest", "dest@dest.email");
@@ -53,7 +48,6 @@ void simple() throws IOException {

@Test
void withHeader() throws IOException {
log.info("Hello");
try (var server = new SMTPServer()) {
var sender = EmailAddress.from("Test", "test@test.email");
var author = EmailAddress.from("Auth", "auth@test.email");
@@ -73,7 +67,6 @@ void withHeader() throws IOException {
@Test
@DisabledOnOs(OS.WINDOWS)
void encoded() throws IOException {
log.info("Hello");
try (var server = new SMTPServer()) {
var sender = EmailAddress.from("Señor Dévèlöper", "test@test.email");
var recipient = EmailAddress.from("Dêst", "dest@dest.email");
@@ -90,7 +83,6 @@ void encoded() throws IOException {

@Test
void timeout() throws IOException {
log.info("Hello");
try (var server = new SMTPServer()) {
var sender = EmailAddress.from("Test", "test@test.email");
var recipient = EmailAddress.from("Dest", "dest@dest.email");
@@ -99,4 +91,17 @@ void timeout() throws IOException {
assertThrows(RuntimeException.class, () -> SMTP.send(server.address(), recipient, sentMail, Duration.ZERO));
}
}

@Test
void withDot() throws IOException {
try (var server = new SMTPServer()) {
var sender = EmailAddress.from("Test", "test@test.email");
var recipient = EmailAddress.from("Dest", "dest@dest.email");
var sentMail = Email.create(sender, "Subject", "Body\n.\nMore text").recipient(recipient).build();

SMTP.send(server.address(), recipient, sentMail);
var email = server.receive(Duration.ofSeconds(10));
assertEquals(sentMail, email);
}
}
}
@@ -71,6 +71,9 @@ private void handleSession(SMTPSession session) throws IOException {
inHeader = false;
}
}
if (line.startsWith(".")) {
line = line.substring(1);
}
mailBody.append(line);
mailBody.append("\n");
}

0 comments on commit b1e3d87

Please sign in to comment.
You can’t perform that action at this time.