Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

151: Encode single dot characters in the SMTP client #240

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions email/src/main/java/org/openjdk/skara/email/SMTP.java
Expand Up @@ -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));
Expand Down Expand Up @@ -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");
}
Expand Down
23 changes: 14 additions & 9 deletions email/src/test/java/org/openjdk/skara/email/SMTPTests.java
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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);
}
}
}
3 changes: 3 additions & 0 deletions test/src/main/java/org/openjdk/skara/test/SMTPServer.java
Expand Up @@ -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");
}
Expand Down