Skip to content

Commit

Permalink
Email body should not be mime encoded, it should use a content-type h…
Browse files Browse the repository at this point in the history
…eader
  • Loading branch information
Robin Westberg committed Sep 30, 2019
1 parent 527967e commit 642a41a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
3 changes: 2 additions & 1 deletion email/src/main/java/org/openjdk/skara/email/Email.java
Expand Up @@ -111,10 +111,11 @@ public static Email parse(String raw) {
.filter(entry -> !entry.getKey().equalsIgnoreCase("From"))
.filter(entry -> !entry.getKey().equalsIgnoreCase("Sender"))
.filter(entry -> !entry.getKey().equalsIgnoreCase("To"))
.filter(entry -> !entry.getKey().equalsIgnoreCase("Content-type"))
.collect(Collectors.toMap(Map.Entry::getKey,
entry -> MimeText.decode(entry.getValue())));

return new Email(id, date, recipients, author, sender, subject, MimeText.decode(message.body), filteredHeaders);
return new Email(id, date, recipients, author, sender, subject, message.body, filteredHeaders);
}

public static EmailBuilder create(EmailAddress author, String subject, String body) {
Expand Down
8 changes: 5 additions & 3 deletions email/src/main/java/org/openjdk/skara/email/SMTP.java
Expand Up @@ -24,6 +24,7 @@

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;

Expand All @@ -47,8 +48,8 @@ public static void send(String server, EmailAddress recipient, Email email) thro
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 out = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8);
var in = new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)) {

var session = new SMTPSession(in, out);

Expand All @@ -66,8 +67,9 @@ public static void send(String server, EmailAddress recipient, Email email) thro
session.sendCommand(header + ": " + MimeText.encode(email.headerValue(header)));
}
session.sendCommand("Subject: " + MimeText.encode(email.subject()));
session.sendCommand("Content-type: text/plain; charset=utf-8");
session.sendCommand("");
session.sendCommand(MimeText.encode(email.body()));
session.sendCommand(email.body());
session.sendCommand(".", doneReply);
session.sendCommand("QUIT");
}
Expand Down
25 changes: 21 additions & 4 deletions test/src/main/java/org/openjdk/skara/test/SMTPServer.java
Expand Up @@ -43,6 +43,7 @@ public class SMTPServer implements AutoCloseable {
private static Pattern quitPattern = Pattern.compile("^QUIT$");

private final static Pattern encodeQuotedPrintablePattern = Pattern.compile("([^\\x00-\\x7f]+)");
private final static Pattern headerPattern = Pattern.compile("[^A-Za-z0-9-]+: .+");

private class AcceptThread implements Runnable {
private void handleSession(SMTPSession session) throws IOException {
Expand All @@ -54,11 +55,27 @@ private void handleSession(SMTPSession session) throws IOException {
var message = session.readLinesUntil(messageEndPattern);
session.sendCommand("250 MESSAGE OK", quitPattern);

// SMTP is only 7-bit safe, ensure that we break any high ascii passing through here
var quoteMatcher = encodeQuotedPrintablePattern.matcher(String.join("\n", message));
var ascii7message = quoteMatcher.replaceAll(mo -> "HIGH_ASCII");
// Email headers are only 7-bit safe, ensure that we break any high ascii passing through
var inHeader = true;
var mailBody = new StringBuilder();
for (var line : message) {
if (inHeader) {
var headerMatcher = headerPattern.matcher(line);
if (headerMatcher.matches()) {
var quoteMatcher = encodeQuotedPrintablePattern.matcher(String.join("\n", message));
var ascii7line = quoteMatcher.replaceAll(mo -> "HIGH_ASCII");
mailBody.append(ascii7line);
mailBody.append("\n");
continue;
} else {
inHeader = false;
}
}
mailBody.append(line);
mailBody.append("\n");
}

var email = Email.parse(ascii7message);
var email = Email.parse(mailBody.toString());
emails.addLast(email);
}

Expand Down

0 comments on commit 642a41a

Please sign in to comment.