SMTP: last CRLF is not included in message content
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1170 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
0d060ca1f0
commit
bf5c6f81a6
|
@ -57,7 +57,7 @@
|
||||||
|
|
||||||
<target name="compile" depends="init">
|
<target name="compile" depends="init">
|
||||||
<mkdir dir="target/classes"/>
|
<mkdir dir="target/classes"/>
|
||||||
<javac srcdir="src/java" destdir="target/classes" source="1.5" target="1.5" debug="on">
|
<javac srcdir="src/java" destdir="target/classes" source="1.5" target="1.5" debug="on" encoding="UTF-8">
|
||||||
<classpath>
|
<classpath>
|
||||||
<path refid="classpath"/>
|
<path refid="classpath"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|
|
@ -934,8 +934,12 @@ public abstract class ExchangeSession {
|
||||||
recipientBuffer.append(line);
|
recipientBuffer.append(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mailBuffer.append(line).append((char) 13).append((char) 10);
|
String nextLine = reader.readLine();
|
||||||
line = reader.readLine();
|
mailBuffer.append(line);
|
||||||
|
if (!".".equals(nextLine)) {
|
||||||
|
mailBuffer.append((char) 13).append((char) 10);
|
||||||
|
}
|
||||||
|
line = nextLine;
|
||||||
}
|
}
|
||||||
// remove visible recipients from list
|
// remove visible recipients from list
|
||||||
List<String> visibleRecipients = new ArrayList<String>();
|
List<String> visibleRecipients = new ArrayList<String>();
|
||||||
|
|
|
@ -21,13 +21,17 @@ package davmail.smtp;
|
||||||
import davmail.AbstractDavMailTestCase;
|
import davmail.AbstractDavMailTestCase;
|
||||||
import davmail.DavGateway;
|
import davmail.DavGateway;
|
||||||
import davmail.Settings;
|
import davmail.Settings;
|
||||||
|
import davmail.exchange.ExchangeSession;
|
||||||
import davmail.exchange.ExchangeSessionFactory;
|
import davmail.exchange.ExchangeSessionFactory;
|
||||||
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
import javax.mail.Session;
|
import javax.mail.Session;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
import java.io.*;
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.BufferedOutputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,28 +39,49 @@ import java.net.Socket;
|
||||||
*/
|
*/
|
||||||
public class TestSmtp extends AbstractDavMailTestCase {
|
public class TestSmtp extends AbstractDavMailTestCase {
|
||||||
static Socket clientSocket;
|
static Socket clientSocket;
|
||||||
static BufferedWriter socketWriter;
|
static BufferedOutputStream socketOutputStream;
|
||||||
static BufferedReader socketReader;
|
static BufferedInputStream socketInputStream;
|
||||||
|
static final byte[] CRLF = {13, 10};
|
||||||
|
enum State {CHAR, CR, CRLF}
|
||||||
|
|
||||||
protected void write(String line) throws IOException {
|
protected void write(String line) throws IOException {
|
||||||
socketWriter.write(line);
|
socketOutputStream.write(line.getBytes("ASCII"));
|
||||||
socketWriter.flush();
|
socketOutputStream.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void writeLine(String line) throws IOException {
|
protected void writeLine(String line) throws IOException {
|
||||||
socketWriter.write(line);
|
write(line);
|
||||||
socketWriter.newLine();
|
socketOutputStream.write(CRLF);
|
||||||
socketWriter.flush();
|
socketOutputStream.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String readLine() throws IOException {
|
protected String readLine() throws IOException {
|
||||||
return socketReader.readLine();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
State state = State.CHAR;
|
||||||
|
while (!(state == State.CRLF)) {
|
||||||
|
int character = socketInputStream.read();
|
||||||
|
if (state == State.CHAR) {
|
||||||
|
if (character == 13) {
|
||||||
|
state = State.CR;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (character == 10) {
|
||||||
|
state = State.CRLF;
|
||||||
|
} else {
|
||||||
|
state = State.CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (state == State.CHAR) {
|
||||||
|
baos.write(character);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new String(baos.toByteArray(), "ASCII");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String readFullAnswer(String prefix) throws IOException {
|
protected String readFullAnswer(String prefix) throws IOException {
|
||||||
String line = socketReader.readLine();
|
String line = readLine();
|
||||||
while (!line.startsWith(prefix)) {
|
while (!line.startsWith(prefix)) {
|
||||||
line = socketReader.readLine();
|
line = readLine();
|
||||||
}
|
}
|
||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
@ -68,8 +93,8 @@ public class TestSmtp extends AbstractDavMailTestCase {
|
||||||
// start gateway
|
// start gateway
|
||||||
DavGateway.start();
|
DavGateway.start();
|
||||||
clientSocket = new Socket("localhost", Settings.getIntProperty("davmail.smtpPort"));
|
clientSocket = new Socket("localhost", Settings.getIntProperty("davmail.smtpPort"));
|
||||||
socketWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
|
socketOutputStream = new BufferedOutputStream(clientSocket.getOutputStream());
|
||||||
socketReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
socketInputStream = new BufferedInputStream(clientSocket.getInputStream());
|
||||||
}
|
}
|
||||||
if (session == null) {
|
if (session == null) {
|
||||||
session = ExchangeSessionFactory.getInstance(Settings.getProperty("davmail.username"), Settings.getProperty("davmail.password"));
|
session = ExchangeSessionFactory.getInstance(Settings.getProperty("davmail.username"), Settings.getProperty("davmail.password"));
|
||||||
|
@ -77,21 +102,25 @@ public class TestSmtp extends AbstractDavMailTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBanner() throws IOException {
|
public void testBanner() throws IOException {
|
||||||
String banner = socketReader.readLine();
|
String banner = readLine();
|
||||||
assertNotNull(banner);
|
assertNotNull(banner);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testLogin() throws IOException {
|
public void testLogin() throws IOException {
|
||||||
String credentials = (char) 0+Settings.getProperty("davmail.username")+ (char) 0 +Settings.getProperty("davmail.password");
|
String credentials = (char) 0+Settings.getProperty("davmail.username")+ (char) 0 +Settings.getProperty("davmail.password");
|
||||||
writeLine("AUTH PLAIN " + new String(new Base64().encode(credentials.getBytes())));
|
writeLine("AUTH PLAIN " + new String(new Base64().encode(credentials.getBytes())));
|
||||||
assertEquals("235 OK Authenticated", socketReader.readLine());
|
assertEquals("235 OK Authenticated", readLine());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testSendMessage() throws IOException, MessagingException {
|
public void testSendMessage() throws IOException, MessagingException, InterruptedException {
|
||||||
|
String body = "Test message\r\n" +
|
||||||
|
"Special characters: éèçà\r\n" +
|
||||||
|
"Chinese: "+((char)0x604F)+((char)0x7D59);
|
||||||
MimeMessage mimeMessage = new MimeMessage((Session) null);
|
MimeMessage mimeMessage = new MimeMessage((Session) null);
|
||||||
mimeMessage.addHeader("To", Settings.getProperty("davmail.to"));
|
mimeMessage.addHeader("To", Settings.getProperty("davmail.to"));
|
||||||
mimeMessage.setText("Test message");
|
mimeMessage.setText(body, "UTF-8");
|
||||||
|
//mimeMessage.setHeader("Content-Transfer-Encoding", "8bit");
|
||||||
mimeMessage.setSubject("Test subject");
|
mimeMessage.setSubject("Test subject");
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
mimeMessage.writeTo(baos);
|
mimeMessage.writeTo(baos);
|
||||||
|
@ -102,9 +131,18 @@ public class TestSmtp extends AbstractDavMailTestCase {
|
||||||
readLine();
|
readLine();
|
||||||
writeLine("DATA");
|
writeLine("DATA");
|
||||||
assertEquals("354 Start mail input; end with <CRLF>.<CRLF>", readLine());
|
assertEquals("354 Start mail input; end with <CRLF>.<CRLF>", readLine());
|
||||||
writeLine(new String(content));
|
mimeMessage.writeTo(socketOutputStream);
|
||||||
|
writeLine("");
|
||||||
writeLine(".");
|
writeLine(".");
|
||||||
assertEquals("250 Queued mail for delivery", readLine());
|
assertEquals("250 Queued mail for delivery", readLine());
|
||||||
|
// wait for asynchronous message send
|
||||||
|
Thread.sleep(1000);
|
||||||
|
ExchangeSession.MessageList messages = session.searchMessages("Sent", session.headerEquals("message-id", mimeMessage.getMessageID()));
|
||||||
|
assertEquals(1, messages.size());
|
||||||
|
ExchangeSession.Message message = messages.get(0);
|
||||||
|
message.getMimeMessage().writeTo(System.out);
|
||||||
|
assertEquals(body, (String) message.getMimeMessage().getDataHandler().getContent());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBccMessage() throws IOException, MessagingException {
|
public void testBccMessage() throws IOException, MessagingException {
|
||||||
|
|
Loading…
Reference in New Issue