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:[email protected]
250 Ok
rcpt to:[email protected]
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?
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:[email protected]
250 Ok
rcpt to:[email protected]
250 Ok
data
354 End data with <CR><LF>.<CR><LF>
From: Test <[email protected]>
To: Burb <[email protected]>
Subject: My fantastic message
test1
test2
test3
.
250 Ok
quit
221 Bye