Tinus Tate Tinus Tate - 7 months ago 31
Java Question

MimeMessage empty when using telnet?

I create a javamail MimeMessage from a inputstream:

MimeMessage message = new MimeMessage(session, data);
Object content = message.getContent();
log.debug("content: "+content);


When i use thunderbird to send the email the content object contains the email text.

But when i use telnet to send email the content object is empty.

220 192.168.2.3 ESMTP SubEthaSMTP 3.1.7
mail from:test@test.com
250 Ok
rcpt to:burb@burb.com
250 Ok
data
354 End data with <CR><LF>.<CR><LF>
test1
test2
test3
.
250 Ok
quit
221 Bye


Connection to host lost.


I do however can get content by manually reading the inputstream:

public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}


Is it possible to detect when to use the MimeMessage and when to use convertStreamToString?

Answer

You aren't sending the mail correctly through SMTP. The message data is usually a set of key/value pairs, followed by a newline, followed by the content. The reason your message is saying that there's no content is that your data is appearing to be all headers.

Your telnet session should look like:

220 192.168.2.3 ESMTP SubEthaSMTP 3.1.7
mail from:test@test.com
250 Ok
rcpt to:burb@burb.com
250 Ok
data
354 End data with <CR><LF>.<CR><LF>
From: Test <test@test.com>
To: Burb <burb@burb.com>
Subject: My fantastic message

test1
test2
test3
.
250 Ok
quit
221 Bye